I'm trying to parse an XML document which contains an optional field in python 2.7.9 using xml.etree.ElementTree.
When I use the following loop:
summary = curTag.find('summary')
if summary:
summary = curTag.find('summary').text.encode('utf8')
summary = summary.replace("'","'||chr(39)||'")
summary = summary[:4000]
I get the the error - AttributeError: 'NoneType' object has no attribute 'text'
However it works when i use:
summary = curTag.find('summary')
if summary:
summary = curTag.find('summary').text.encode('utf8')
summary = summary.replace("'","'||chr(39)||'")
summary = summary[:4000]
However, I get a warning - FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead. if summary.text is not None:
I've just moved to python from R and am trying to figure out why this is happening.
Adding the exact error:
Traceback (most recent call last):
File "", line 1, in runfile('D:/XML_Parsev1.0.py', wdir='D:')
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile execfile(filename, namespace)
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 66, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)
File "D:/XML_Parsev1.0.py", line 69, in summary = curTag.find('summary').text.encode('utf8')
AttributeError: 'NoneType' object has no attribute 'encode'
No comments:
Post a Comment