Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List can not be serialized error when using Xpath with lxml etree

I am trying to search for a string within an XML document, and then print out the entire element, or elements, that contain that string. This is my code so far:

post = open('postf.txt', 'r')
postf = str(post.read())

root = etree.fromstring(postf)

e = root.xpath('//article[contains(text(), "stuff")]')

print etree.tostring(e, pretty_print=True)

This is the XML that is being searched from postf.txt

<stuff>

<article date="2014-05-18 17:14:44" title="Some stuff">More testing
debug
[done]
<tags>Hello and stuff
</tags></article>

</stuff>

And finally, this is my error:

  File "cliassis-1.2.py", line 107, in command
    print etree.tostring(e, pretty_print=True)
  File "lxml.etree.pyx", line 3165, in lxml.etree.tostring (src\lxml\lxml.etree.c:69414)
TypeError: Type 'list' cannot be serialized.

What I want this to do, is search for all elements containing the string I searched for, and then print out the tags. So if I have test and stuff, and I search for 'test', I want it to print out "test and stuff

like image 856
James Avatar asked Feb 12 '23 22:02

James


1 Answers

articles = root.xpath('//article[contains(text(), "stuff")]')

for article in articles:
    print etree.tostring(article, pretty_print=True)

root.xpath returns a Python list. So e is a list. etree.tostring converts lxml _Elements to strings; it does not convert lists of _Elements to strings. So use a for-loop to print the _Elements inside the list as strings.

like image 199
unutbu Avatar answered Feb 16 '23 02:02

unutbu