在带有 lxml 和 python 2.7 的 xpath 中使用 unicode 作为谓词

Use unicode as predicate in xpath with lxml and python 2.7

我一直面临这样的问题,我有一个带有 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)

停止混合它们。

xpath = u"//language[./description = '{}']//description/text()".format(description)