unicorn
unicorn

Reputation: 139

Use unicode as predicate in xpath with lxml and python 2.7

I've been facing the problem where I have an XML file with Unicode strings and need to evaluate an Xpath on it, through lxml in Python-2.7.

# -*- 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")

Gives error: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 14: ordinal not in range(128)

Upvotes: 0

Views: 244

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798746

Stop mixing them.

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

Upvotes: 1

Related Questions