Я пытаюсь очистить список атрибутов тега в файле xml
. Но мне не удалось очистить список атрибутов тега.
Например, мои данные выглядят как
<sentences>
<sentence id="1126814:0">
<Opinions>
<Opinion target="Leon" category="RESTAURANT#GENERAL" polarity="positive" from="0" to="4"/>
<Opinion target="Leon" category="AMBIENCE#GENERAL" polarity="positive" from="0" to="4"/>
<Opinion target="specials" category="FOOD#QUALITY" polarity="positive" from="95" to="103"/>
<Opinion target="atmosphere" category="AMBIENCE#GENERAL" polarity="positive" from="123" to="133"/>
<Opinion target="French bistro fare" category="FOOD#QUALITY" polarity="positive" from="70" to="88"/>
</Opinions>
</sentence>
</sentences>
Я хочу очистить каждую категорию в теге мнения.
category_list = ('1126814:0',[('RESTAURANT#GENERAL','positive')], [('AMBIENCE#GENERAL', 'positive')], [('FOOD#QUALITY', 'positive')], [('AMBIENCE#GENERAL', 'positive')])
Я попробовал следующий код:
from bs4 import BeautifulSoup
with open("new2.xml", "r") as file:
url = file.read()
soup = BeautifulSoup(url, "html.parser")
required0 = soup.find_all("sentences")
text1 = []
for i in required0:
sent_id = []
category = []
for sentences in soup.find_all('sentence'):
if sentences.has_attr('id'):
sent_id.append(sentences['id'])
text.append(sentences.get_text())
for opinions in soup.find_all('opinion'):
cat1 = []
pola1 = []
targ = []
if opinions.has_attr('category'):
cat1.append(opinions['category'])
if opinions.has_attr('polarity'):
pola1.append(opinions['polarity'])
if opinions.has_attr('target'):
targ.append(opinions['target'])
cat_list = list(zip(cat1,pola1,targ))
category.append(cat_list)
catlist= list(zip(sent_id, category, category))
text1.append(catlist)
Я получаю вывод следующим образом:
[[('1126814:0',
[('RESTAURANT#GENERAL', 'positive', 'Leon')],
[('RESTAURANT#GENERAL', 'positive', 'Leon')])]]
Заранее благодарен за любую помощь.