我正在尋找或者忽略我的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