为什么我的XML解析代码不起作用(Python)

时间:2015-12-02 22:27:01

标签: python xml-parsing elementtree

下面是我试图从中提取信息的XML文件的部分部分,我得到的结果是“无”10次(我的XML文件中只有10条记录)。我不确定问题是什么?我在本文末尾复制了代码。

<?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 个答案:

答案 0 :(得分:1)

从XML文件中检索数据的一个常见问题是,您不在自己认为的节点上。

所以确认你的假设。打印节点名称(而不是文本)以确认您所在的节点。

如果您遇到特定记录问题,请简化您的问题,将XML文件缩减为该记录并进行测试(再次打印节点)。 XML中可能存在一些不同的东西,导致代码无法正常工作(格式不正确,或者结构不同或数据不同)。

您在上面遇到的一个问题是......

print(children.text)

如果节点是父节点(并且没有文本),

将不打印任何内容。一个例子是TITLES标签。此标记没有文本,只有子节点。子节点具有文​​本。因此,您需要导航到子节点以访问TITLE中的文本。