Google' root = obj" /> Google' root = obj" /> Google' root = obj"/>

Parsing XML with lxml.objectify.Getting error

116 Views Asked by At

The following code is from 'Python for Data Analysis':

from io import StringIO
import io

tag = '<a href="http://www.google.com">Google</a>'
root = objectify.parse(io.StringIO(tag).getroot())

Executing the code produces the following error:

TypeError: initial_value must be unicode or None, not str

Please help!

1

There are 1 best solutions below

0
merlyn On

StringIO expects a unicode string which is not the default kind of string in python 2. Replacing

tag = '<a href="http://www.google.com">Google</a>'

with

tag = u'<a href="http://www.google.com">Google</a>'

should work.