The following code will read in the contents of the file into a string, and pass it into the class instantiator for BytesIO
xml = BytesIO(open("/home/user1/test.xml").read())
Question
I have the following line of code: xml = BytesIO("<A><B>some text</B></A>")
for the file named test.xml
.
But I would like to have something like xml = "/home/user1/test.xml"
How can I use the file location instread of having to put the file content?
Solution 2
The following code will read in the contents of the file into a string, and pass it into the class instantiator for BytesIO
xml = BytesIO(open("/home/user1/test.xml").read())
OTHER TIPS
Exactly like you have. lxml.etree.parse()
accepts a string filename and will read the file for you.
xml = open('/home/user1/test.xml', 'rb').read()