我想將我的 XML 文件轉換為數據框熊貓我試過這段代碼
import pandas as pd
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("C:/Users/user/Desktop/essai/dataXml.xml", "r"),"xml")
d = {}
for tag in soup.RECORDING.find_all(recursive=False):
d[tag.name] = tag.get_text(strip=True)
df = pd.DataFrame([d])
print(df)
這是我的 XML 數據的一部分
<?xml version="1.0" encoding="utf-8"?>
<sentences>
<sentence>
<text>We went again and sat at the bar this time, I had 5 pints of guinness and not one buy-back, I ordered a basket of onion rings and there were about 5 in the basket, the rest was filled with crumbs, the chili was not even edible.</text>
<aspectCategories>
<aspectCategory category="place" polarity="neutral"/>
<aspectCategory category="food" polarity="negative"/>
</aspectCategories>
</sentence>
</sentences>`
我收到了這個錯誤
for tag in soup.RECORDING.find_all(recursive=False):
AttributeError: 'NoneType' object has no attribute 'find_all'
我該如何解決?
提前謝謝你
編輯:替換soup.RECORDING.find_all
為soup.find_all
修復了錯誤,但我仍然沒有得到我想要的
試試這個代碼:
d = {
'text': [],
'aspect': [],
'polarity': []
}
for sentence in soup.find_all('sentence'):
text = sentence.find('text').text
for ac in sentence.find_all('aspectCategory'):
d['text'].append(text)
d['aspect'].append(ac.get('category'))
d['polarity'].append(ac.get('category'))
df = pd.DataFrame(d)
輸出:
>>> df
text aspect polarity
0 We went again and sat at the bar this time, I ... place place
1 We went again and sat at the bar this time, I ... food food
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句