简体   繁体   中英

Right way to use lxml to generate xml in python

I'm trying to create xml that corresponds to structure of directory (with subdirectories and files in that subdirs). When I try to use this example: Best way to generate xml? instead of output from example, that is:

<root>
  <child/>
  <child>some text</child>
</root>

I've got:

b'<root>\n  <child/>\n  <child>some text</child>\n</root>\n'

Why is it so? Uses PyCharm IDE if it matters.

'\\n' is a Line feed char (LF).

When you, for instance, save your output to a file and open it in some editor, you'll get what you expect.

Your output is fine.

This is a change in Python 3. etree.tostring() is returning a bytes literal, which is denoted by b'string value' . Note that whenever you see \\n in the bytes literal it means the same as a new line if you print it out to a file.

You can turn this into a regular string using s = s.decode('utf-8') .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM