2015-12-02 49 views
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> 
+1

尝试为每个孩子打印出节点的名称(而不是文本),然后为每个孩子确保你在树中的正确位置。 – Quantumplate

+0

Quantumplate,你是对的,我在错误的地方,我固定的位置,但是,我有一些奇怪的结果,我能够从10条记录中检索数据,然后我得到一个错误。即使我能够检索到的数据也缺少书名。我上面更新了我的帖子。 –

+0

看起来,如果有一个集合(例如list,titles节点是一个示例),您将获得None。您需要访问这些子节点才能获取文本。 – Quantumplate

回答

1

从一个XML文件中检索数据的一个共同问题是,你不觉得你是在节点上。

因此请确认您的假设。打印节点名称(而不是文本)以确认您正在使用哪个节点。

如果您遇到特定记录问题,然后简化问题,请将您的XML文件简化为该记录并测试(再次打印节点)。这可能导致您的代码不能正常工作(这是错误的,或者它具有不同的结构或不同的数据)。你在上面有

一个问题是......

打印(children.text)

将打印咱这节点是母(和无正文)。 TITLES标签就是一个例子。这个标签没有文本,只是一个子节点。该子节点具有文​​本。因此,您需要导航到子节点才能访问TITLE中的文本。

相关问题