私はいくつかのxmlを持っています:
<article>
<uselesstag></uslesstag>
<topic>oil, gas</topic>
<body>body text</body>
</article>
<article>
<uselesstag></uslesstag>
<topic>food</topic>
<body>body text</body>
</article>
<article>
<uselesstag></uslesstag>
<topic>cars</topic>
<body>body text</body>
</article>
意味のないタグがたくさんあります。Beautifulsoup を使用して body タグ内のすべてのテキストとそれに関連するトピック テキストを収集し、新しい xml を作成したいと考えています。
私はpythonを初めて使用しますが、何らかの形であると思われます
import arff
from xml.etree import ElementTree
import re
from StringIO import StringIO
import BeautifulSoup
from BeautifulSoup import BeautifulSoup
totstring=""
with open('reut2-000.sgm', 'r') as inF:
for line in inF:
string=re.sub("[^0-9a-zA-Z<>/\s=!-\"\"]+","", line)
totstring+=string
soup = BeautifulSoup(totstring)
body = soup.find("body")
for anchor in soup.findAll('body'):
#Stick body and its topics in an associated array?
file.close
動作します。
1) どうすればいいですか?2) XML にルート ノードを追加する必要がありますか? そうでなければ、それは適切な XML ではありませんか?
どうもありがとう
編集:
私が終わらせたいのは:
<article>
<topic>oil, gas</topic>
<body>body text</body>
</article>
<article>
<topic>food</topic>
<body>body text</body>
</article>
<article>
<topic>cars</topic>
<body>body text</body>
</article>
意味のないタグがたくさんあります。