我一直面临着这样的问题:我有一个带有Unicode字符串的XML文件,需要通过Python-2.7中的lxml来评估它上面的Xpath。
# -*- coding: utf-8 -*-
from lxml import etree
...
class Language:
description = None
def __init__(self, description):
xpath = "//language[./description = '{}']//description/text()".format(description)
self.description= lang_xml.xpath(xpath)
...
lang = Language(u"Norwegian Bokmål")
给出错误:UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 14: ordinal not in range(128)
答案 0 :(得分:1)
停止混合它们。
xpath = u"//language[./description = '{}']//description/text()".format(description)