Hello,
I made the mistake of using soup.prettify() to save soups to files, and I now have whitespaces that show up as useless spaces when viewing the files in an HTML WYSIWYG editor.
The following code doesn't work to remove those useless whitespaces.
Before I write a Python script to run the files through Tidy instead, does someone know if it can be fixed with BS?
Thank you.
I made the mistake of using soup.prettify() to save soups to files, and I now have whitespaces that show up as useless spaces when viewing the files in an HTML WYSIWYG editor.
The following code doesn't work to remove those useless whitespaces.
Before I write a Python script to run the files through Tidy instead, does someone know if it can be fixed with BS?
Thank you.
for file in glob.glob("*.html"): BASE = Path(file).stem OUTPUTFILE = fr"{BASE}.CONV.html" soup = BeautifulSoup(open(file,"br"),"lxml") for tag in soup.find_all(): if tag.string: tag.string.replace_with(' '.join(tag.string.split())) print(tag.string) else: print(tag.name, " no string") pass with open(OUTPUTFILE, 'w', encoding='utf-8') as outp: outp.write(str(soup))