2020-03-15发表2020-03-15更新Python几秒读完 (大约60个字)Python格式化xml文件在使用Python构建XML文件的时候,默认是没有缩减的。 发现BeautifulSoup自带自动缩减 方法如下: 12345from bs4 import BeautifulSoupbs = BeautifulSoup(open(tmp_file), 'xml')with open(filename, 'w', encoding='utf-8') as f: f.write(bs.prettify()) #xml