在xpath中使用unicode作为谓词,使用lxml和python 2.7

时间:2015-10-15 07:41:00

标签: python-2.7 xpath unicode lxml

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

1 个答案:

答案 0 :(得分:1)

停止混合它们。

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