how to pretty_print xincluded file?

from lxml import etree tree = etree.parse("part_000001.xml") tree.xinclude() string = etree.tostring(tree, pretty_print=True)  print(string) 

I’m trying to pretty_print an XML file, my pretty_print option is turned on in the console (%%pprint to turn it on and off), but the terminal still doesn’t insert an actual newline and instead "\n" is included in the string.

How to change it so that the newline is actually inserted?

Add Comment
2 Answer(s)

U can try BeautifulSoup and using prettify() u can format your code as so:

from bs4 import BeautifulSoup  #you will FIRST need to read the xml file to pass into BS as shown below  content = [] with open("part_000001.xml", "r") as file:     content = file.readlines()     content = "".join(content)     bs_content = bs(content, "lxml")  print(bs_content.prettify()) 

You can read more on this here.

Add Comment

the bytes must first be turned into a string:

string = etree.tostring(tree, pretty_print=True).decode("utf-8") #decode will convert bytes into string 
Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.