Python格式化xml文件

本文最后更新于 2020年3月15日 晚上

在使用Python构建XML文件的时候,默认是没有缩减的。

发现BeautifulSoup自带自动缩减

方法如下:

1
2
3
4
5
6
from bs4 import BeautifulSoup
bs = BeautifulSoup(open(tmp_file), 'xml')

with open(filename, 'w', encoding='utf-8') as f:
f.write(bs.prettify())


Python格式化xml文件
https://yuluod.github.io/2020/03/15/Python格式化xml文件/
作者
yuluo
发布于
2020年3月15日
许可协议