解析XML文件時,我有這樣的XML文件:獲得NoneType錯誤在Python
<dep type="nsubj">
<governor idx="7">open</governor>
<dependent idx="5">it</dependent>
</dep>
<dep type="aux">
<governor idx="7">open</governor>
<dependent idx="6">will</dependent>
</dep>
<dep type="ccomp">
<governor idx="3">announced</governor>
<dependent idx="7">open</dependent>
</dep>
我想分析它,並提取深型,即像nsubj,AUX,CCOMP等我」 m這樣做:
file_list=[]
with open(xml_file) as f:
page = f.read()
f.close()
soup = BeautifulSoup(page,"xml")
for types in soup.find_all('dep'):
file_list.append(types.string.strip())
print file_list
但是,我得到NoneType錯誤。爲什麼這樣?
編輯:
回溯:
Traceback (most recent call last):
File "/Users/akritibahal/Downloads/stanford-corenlp-2012-07-09/testing.py", line 103, in <module>
main()
File "/Users/akritibahal/Downloads/stanford-corenlp-2012-07-09/testing.py", line 102, in main
extract_top_dependencies('/Users/akritibahal/Downloads/stanford-corenlp-2012-07-09/test')
File "/Users/akritibahal/Downloads/stanford-corenlp-2012-07-09/testing.py", line 80, in extract_top_dependencies
file_list.append(types.string.strip())
AttributeError: 'NoneType' object has no attribute 'strip'
EDIT2:
我認爲它是怎麼把我怎麼一直在做的XML解析是它的<>這些標記之間的讀取。但對於dep,我想提取type =中的內容,並且打開和關閉標記之間沒有任何內容。怎麼做?
請編輯完整的回溯,你得到的NoneType錯誤到你的問題。 – Marius 2014-11-04 04:01:58
哪條線導致此? – Eric 2014-11-04 04:02:52