So my XML file goes somewhat like this:
<Root>
<S_CellBody Id = "16393">
<?OldID 16393?>
<ph Id = "19393">SELx</ph>
(x=0)
</S_CellBody>
</Root>
I want to extract (x=0) form this XML using ElementTree Library in python I am trying to access it using my below code:
(say I read this XML from a file in a variable 'tree') Python 3.5 code:
root= tree.getroot()
s_cellbody= root.find('.//S_CellBody').text
print(s_cellbody)
But this above code gives me the output 'None'
I don't understand what is happening coz '(x=0)' is the text under the tag 'S_CellBody'. Can anyone explain this!!!!
EDIT1: S_cellBody was just a typo! sorry I have corrected it to 'S_CellBody'
You have to take the tail of that element.
Please check the code from ipython console below,
In [1]: import xml.etree.ElementTree as ET
In [2]: cat myxml.xml
<Root>
<S_CellBody Id = "16393">
<?OldID 16393?>
<ph Id = "19393">SELx</ph>
(x=0)
</S_CellBody>
</Root>
In [3]: tree = ET.parse('myxml.xml')
In [4]: root = tree.getroot()
In [5]: elem = root.find('S_CellBody')
In [6]: if elem:
...: print(elem[0].tail)
...:
/usr/local/bin/ipython:1: FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead.
#!/usr/bin/python
(x=0)
In [7]: if elem is not None:
...: print(elem[0].tail)
...:
(x=0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.