我正在寻找或者忽略我的xml中的unicode。我愿意以某种方式在输出处理中改变它。用python和lxml忽略xml中的unicode?
我的Python:
import urllib2, os, zipfile
from lxml import etree
doc = etree.XML(item)
docID = "-".join(doc.xpath('//publication-reference/document-id/*/text()'))
target = doc.xpath('//references-cited/citation/nplcit/*/text()')
#target = '-'.join(target).replace('\n-','')
print "docID: {0}\nCitation: {1}\n".format(docID,target)
outFile.write(str(docID) +"|"+ str(target) +"\n")
创建的输出:
docID: US-D0607176-S1-20100105
Citation: [u"\u201cThe birth of Lee Min Ho's donuts.\u201d Feb. 25, 2009. Jazzholic. Apr. 22, 2009 <http://www
但是,如果我尝试在'-'join(target).replace('\n-','')
加回我得到这个错误都print
和outFile.write
:
Traceback (most recent call last):
File "C:\Documents and Settings\mine\Desktop\test_lxml.py", line 77, in <module>
print "docID: {0}\nCitation: {1}\n".format(docID,target)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 0: ordinal not in range(128)
我该如何忽略unicode,这样我就可以输出target
与outFile.write
?
当你从__future__导入unicode_literals时会发生什么? – 2012-03-12 21:20:38