我正在编写一个脚本,该脚本使用BeautifulStoneSoup
编辑XML文件,但库将所有标记转换为小写。有没有办法保存这个案子?
import BeautifulSoup
xml = "<TestTag>a string</TestTag>"
soup = BeautifulSoup.BeautifulStoneSoup(xml, markupMassage=False)
print soup.prettify() # or soup.renderContents()
#prints
>>> <testtag>a string</testtag>
#instead of the expected
>>> <TestTag>a string</TestTag>
最佳答案
您可以使用Beautiful Soup 4,如下所示(需要lxml xml xml库):
In [10]: from bs4 import BeautifulSoup
In [11]: xml = "<TestTag>a string</TestTag>"
In [12]: soup = BeautifulSoup(xml, "xml")
In [13]: print soup
<?xml version="1.0" encoding="utf-8"?>
<TestTag>a string</TestTag>
In [14]: