Python格式化xml文件

在使用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())

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×