17

XMLファイルをで編集するスクリプトを書いていますBeautifulStoneSoupが、ライブラリはすべてのタグを小文字に変換します。ケースを保存するオプションはありますか?

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>
4

1 に答える 1

18

次のように、 Beautiful Soup 4を使用できます(lxml 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]:
于 2012-08-28T16:40:56.467 に答える