Frage

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?

War es hilfreich?

Lösung 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())

Andere Tipps

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()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top