![]() |
[SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? (/thread-38108.html) |
[SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? - Winfried - Sep-03-2022 Hello, One more before I go to bed… No matter what I try, BS turns the brackets of the LS string into "<" and ">" instead of keeping them as is: from bs4 import BeautifulSoup soup = BeautifulSoup(open('input.kml', 'r'), 'xml') LS = "<Style><LineStyle><color>FF0000FF</color><width>6</width></LineStyle></Style>" for ls in soup.find_all("LineString"): parent = ls.parent #BAD parent.insert(0,LS) #BAD parent.insert(0,LS.encode(formatter="html")) #BAD parent.insert(0,str(LS)) print(soup.prettify())Thank you. -- Edit: Found a solution print(soup.prettify(formatter=None)) with open(OUTPUTFILE, "w") as file: #file.write(str(soup)) file.write(soup.prettify(formatter=None)) |