0
下面是一個XML文件的部分部分,我試圖從信息中刪除信息,我得到的結果有10個單詞「無」(我只有我的XML文件中有10條記錄)。我不確定問題是什麼?我在本文結尾處複製了代碼。爲什麼我的XML解析代碼無法正常工作(Python)
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<records>
<record>
<database name="My Collection.enl" path="My Collection.enl">My Collection.enl</database>
<ref-type name="Book">1</ref-type>
<contributors>
<authors>
<author>AIA Research Corporation</author>
</authors>
</contributors>
<titles>
<title>Regional guidelines for building passive energy conserving homes</title>
</titles>
<periodical/>
<keywords/>
<dates>
<year>1978</year>
</dates>
<publisher>Dept. of Housing and Urban Development, Office of Policy Development and Research : for sale by the Supt. of Docs., U.S. Govt. Print. Off.</publisher>
<urls/>
<label>Energy;Green Buildings;High Performance Buildings</label>
</record>
<record>
<database name="My Collection.enl" path="My Collection.enl">My Collection.enl</database>
<ref-type name="Book">1</ref-type>
<contributors>
<authors>
<author>Akinci, Burcu</author>
<author>Ph, D</author>
</authors>
</contributors>
<titles>
<title>Computing in Civil Engineering</title>
</titles>
<periodical/>
<pages>692-699</pages>
<keywords/>
<dates>
<year>2007</year>
</dates>
<publisher>American Society of Civil Engineers</publisher>
<isbn>9780784409374</isbn>
<electronic-resource-num>ISBN 978-0-7844-1302-9</electronic-resource-num>
<urls>
<web-urls>
<url>http://books.google.com/books?id=QigBgc-qgdoC</url>
</web-urls>
</urls>
<label>Computing</label>
</record>
下面是代碼:
import xml.etree.ElementTree as ET
tree =ET.parse('My_Collection.xml')
root = tree.getroot()
for child in root:
for children in child:
print (children.text)
print("\n")
更新,我定我的代碼,但我得到這個奇怪的錯誤消息,也有一些的記錄丟失的書名,下面是更新後的代碼和結果。
import xml.etree.ElementTree as ET
tree =ET.parse('My_Collection.xml')
root = tree.getroot()
for child in root:
for children in child:
for books in children:
print (books.text)
print ('\n')
下面是結果:
My Collection.enl
1
None
None
None
None
None
Dept. of Housing and Urban Development, Office of Policy Development and Research : for sale by the Supt. of Docs., U.S. Govt. Print. Off.
None
Energy;Green Buildings;High Performance Buildings
My Collection.enl
1
None
None
None
692-699
None
None
American Society of Civil Engineers
9780784409374
ISBN 978-0-7844-1302-9
None
Computing
My Collection.enl
0
None
None
None
291-314
4
4
None
None
None
Computing;Design;Green Buildings
My Collection.enl
0
None
None
None
1847-1870
3
9
None
None
10.3390/rs3091847
None
Infrared;Laser scanning
My Collection.enl
0
None
None
None
Nr. 15
15
None
None
ISSN~~1435-618X
ISSN 1435-618X
None
Outdoor Thermal Comfort;Urban Desgin
Traceback (most recent call last):
File "Mend_lib_Xml_Excel.py", line 9, in <module>
print (books.text)
File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\ufffd' in position 679: character maps to <undefined>
C:\Users\Rania\Google Drive\Rania's Documents\EDX and Coursera\Python_Michigan\Course1>
嘗試爲每個孩子打印出節點的名稱(而不是文本),然後爲每個孩子確保你在樹中的正確位置。 – Quantumplate
Quantumplate,你是對的,我在錯誤的地方,我固定的位置,但是,我有一些奇怪的結果,我能夠從10條記錄中檢索數據,然後我得到一個錯誤。即使我能夠檢索到的數據也缺少書名。我上面更新了我的帖子。 –
看起來,如果有一個集合(例如list,titles節點是一個示例),您將獲得None。您需要訪問這些子節點才能獲取文本。 – Quantumplate