Python encoding error when turning MySQL query to XML

Segadora

New Member
I generate custom XML files that have to be in a certain format with this scripts. It queries a database and turns the results into one big xml file. I do this to multiple databases that range from inventory parts list to employee records.\[code\]import csvimport StringIOimport timeimport MySQLdbimport lxml.etreeimport lxml.builderfrom datetime import datetimeimport stringfrom lxml import etreefrom lxml.builder import E as buildEfrom datetime import datetimefrom time import sleepimport shutilimport globimport osimport loggingdef logWrite(message): logging.basicConfig( filename="C:\\logs\\XMLSyncOut.log", level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S: %p' ) logging.debug(message)def buildTag(tag,parent=None,content=None): element = buildE(tag) if content is not None: element.text = unicode(content) if parent is not None: parent.append(element) return elementdef fetchXML(cursor): logWrite("constructing XML from cursor") fields = [x[0] for x in cursor.description] doc = buildTag('DATA') for record in cursor.fetchall(): r = buildTag('ROW',parent=doc) for (k,v) in zip(fields,record): buildTag(k,content=v,parent=r) return docdef updateDatabase 1(): try: conn = MySQLdb.connect(host = 'host',user = 'user',passwd = 'passwd',db = 'database') cursor = conn.cursor() except: sys.exit(1) logWrite("Cannot connect to database - quitting!") cursor.execute("SELECT * FROM database.table") logWrite("Dumping fields from database.table into cursor") xmlFile = open("results.xml","w") doc = fetchXML(cursor) xmlFile.write(etree.tostring(doc,pretty_print=True)) logWrite("Writing XML results.xml") xmlFile.close()\[/code\]For some reason one of the new databases I imported from an excel spreadsheet is having some type of encoding error that that the others aren't having. This is the error\[code\]element.text = unicode(content)UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 21: ordinal not in range(128)\[/code\]I tried explicitly encoding to ascii by changing the buildTag function to look like this:\[code\]def buildTag(tag,parent=None,content=None): element = buildE(tag) if content is not None: content = str(content).encode('ascii','ignore') element.text = content if parent is not None: parent.append(element) return element\[/code\]This still didn't work.Any ideas on what I can do to stop this? I can't escape them because I can't have "\x92" showing up in records as output.
 
Back
Top