Obviously , I’m new to Python.
I would like to use StringIO in my code below : to extract example.xml
import os
os.chdir('d:/py/xml/')
from lxml import etree
from StringIO import StringIO
#----------------------------------------------------------------------
def parseXML(xmlFile):
"""
Parse the xml
"""
f = open(xmlFile)
xml = f.read()
f.close()
tree = etree.parse(StringIO(xml))
context = etree.iterparse(StringIO(xml))
for action, elem in context:
if not elem.text:
text = 'None'
else:
text = elem.text
print (elem.tag + ' => ' + text)
if __name__ == "__main__":
parseXML("example.xml")
But I keep getting this msg
Syntax Error: from io import import StringIO: d:\py\xml\example.py, line 621
File “d:\py\xml\example.py”, line 6, in ?
from io import import StringIO
I did google but it said to import the io model and use io.StringIO or io.BytesIO for text or Data…
Can anyone please tell me, how can I actually do that ?
Thanks
In Python3,
StringIOis inio:(Not
from io import import StringIO, the keywordimportgoes only once.)Note the the
2to3script will do this change for you automatically.