How to make XPath return 'None' in Python if no data found?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 12:24:46

问题


XPath returns nothing if a child element has no text value. In this case, rating has no data, so I want it to say so - None or nothing in this child instead of just ignoring it. Your input is much appreciated.

XML :

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>  
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
  <rating></rating>
</book>

<book>
  <title lang="hindi">Learning XML</title>
  <price>39.95</price>
  <rating></rating>
</book>

</bookstore>

Python :

>>> import lxml.html as lh
>>> bk=open('book.xml','r')
>>> bkout=lh.parse(bk)
>>> bk.close()
>>> bkout.xpath('//book/*/text()')
['Harry Potter', '29.99', 'Learning XML', '39.95']

>>> bkout.xpath('//book/* and not(text())/text()')
True

Desired Output :

['Harry Potter', '29.99', '', 'Learning XML', '39.95', '']
or
['Harry Potter', '29.99', None, 'Learning XML', '39.95', None]

回答1:


Remove the "text()":

In [16]: [x.text for x in bk.xpath("//book/*")]
Out[16]: ['Harry Potter', '29.99', None, 'Learning XML', '39.95', None]


来源:https://stackoverflow.com/questions/11105900/how-to-make-xpath-return-none-in-python-if-no-data-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!