下面给出了我的XML文件示例,我想访问文本“The bread is top notch also”和类别“food”
<sentences>
<sentence id="32897564#894393#2">
<text>The bread is top notch as well.</text>
<aspectTerms>
<aspectTerm term="bread" polarity="positive" from="4" to="9"/>
</aspectTerms>
<aspectCategories>
<aspectCategory category="food" polarity="positive" />
</aspectCategories>
</sentence>
我的密码是
test_text_file=open('Restaurants_Test_Gold.txt', 'rt')
test_text_file1=test_text_file.read()
root = ET.fromstring(test_text_file1)
for page in list(root):
text = page.find('text').text
Category = page.find('aspectCategory')
print ('sentence: %s; category: %s' % (text,Category))
test_text_file.close()
这是我解决你问题的代码
希望有帮助
这取决于XML格式的复杂程度。最简单的方法是直接访问路径
但是如果有类似的标记,您可能需要使用更长的路径,比如
.//aspectCategories/aspectCategory
相关问题 更多 >
编程相关推荐