如何在BeautifulSoup.BeautifulStoneSoup中维护区分大小写的标签? [英] How to maintain case-sensitive tags in BeautifulSoup.BeautifulStoneSoup?
本文介绍了如何在BeautifulSoup.BeautifulStoneSoup中维护区分大小写的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个脚本,该脚本使用BeautifulStoneSoup
编辑XML文件,但是该库将所有标签都转换为小写.有保护案件的选项吗?
I am writing a script that edits an XML file with BeautifulStoneSoup
, but the library converts all tags to lower case. Is there an option to conserve the case?
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>
推荐答案
You could use Beautiful Soup 4, as follows (requires the lxml XML library):
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]:
这篇关于如何在BeautifulSoup.BeautifulStoneSoup中维护区分大小写的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文