Python saving unicode to XML -


i'm writing short python script walk through directories on server, find i'm looking , save data xml file.

the problem of data written in other languages such "ハローワールド" or of similar format. when trying save in entry in xml following traceback:

unicodedecodeerror: 'ascii' codec can't decode byte 0xec in position 18: ordinal not in range(128) 

this function saves data looks like:

def addhistoryentry(self, title, url):     self.log.info('adding history entry {"title":"%s", "url":"%s"}' % (title, url))     root = self.getroot()     history = root.find('.//history')     entry = etree.subelement(history, 'entry')     entry.set('title', title)     entry.set('time', str(unixtime()))     entry.text = url     history.set('results', str(int(history.attrib['results']) + 1))     self.write(root) 

self.getroot() following:

def getroot(self):     return etree.elementtree(file = self.config).getroot() 

and here function writes data (self.write(root))

def write(self, xmlroot):     bump = open(self.config, 'w+')     bump.write(dom.parsestring(etree.tostring(xmlroot, 'utf-8')).toxml())     bump.close() 

imports are:

import xml.etree.elementtree etree import xml.dom.minidom dom 

if can me out issue please do. help.

you may use codecs library of python, , .decode('utf-8') solve it.

import sys,codecs reload(sys) sys.setdefaultencoding("utf-8") 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -