Python使用xmltodict

时间:2017-10-09 16:40:28

标签: python xml xmltodict

XML文件的一部分:

<?xml version="1.0" encoding="UTF-8"?>

<Station>
    <Code>HT</Code>
    <Type>knooppuntIntercitystation</Type>
    <Namen>
        <Kort>Den Bosch</Kort>
        <Middel>'s-Hertogenbosch</Middel>
        <Lang>'s-Hertogenbosch</Lang>
    </Namen>
    <Land>NL</Land>
    <Synoniemen>
        <Synoniem>Hertogenbosch ('s)</Synoniem>
        <Synoniem>Den Bosch</Synoniem>
    </Synoniemen>
</Station>


<Station>
    <Code>ALMO</Code>
    <Type>stoptreinstation</Type>
    <Namen>
        <Kort>Oostvaard</Kort>
        <Middel>Oostvaarders</Middel>
        <Lang>Almere Oostvaarders</Lang>
    </Namen>
    <Land>NL</Land>
    <Synoniemen></Synoniemen>
</Station>

<Station>
    <Code>ATN</Code>
    <Type>stoptreinstation</Type>
    <Namen>
        <Kort>Aalten</Kort>
        <Middel>Aalten</Middel>
        <Lang>Aalten</Lang>
    </Namen>
    <Land>NL</Land>
    <Synoniemen></Synoniemen>
</Station>

<Station>
    <Code>ASA</Code>
    <Type>intercitystation</Type>
    <Namen>
        <Kort>Amstel</Kort>
        <Middel>Amsterdam Amstel</Middel>
        <Lang>Amsterdam Amstel</Lang>
    </Namen>
    <Land>NL</Land>
    <Synoniemen></Synoniemen>
</Station>

我的python代码读取xmlfile:

import xmltodict

def leesXML(filename):
    with open(filename) as mijnXMLBestand:
        inhoud = mijnXMLBestand.read()
        xmldictionary = xmltodict.parse(inhoud)
        return xmldictionary

stationsdictionary = leesXML('stations.xml')
stations = stationsdictionary['Stations']['Station']

def program():
    for station in stations:
        type = station['Type']
        Code = station['Code']
        print(Code + '      -       ' + type)
print('Dit zijn de codes en types van de 4 stations:')
program()

使用Program(),我将获得一个包含xml文件中类型和代码标记的列表。

我现在正在尝试弄清楚如何从中获取lang标签并制作类似上面的类似列表,但只使用Code和Lang标签

我做了一个新变量:

thirdtag = stationsdictionary['Stations']['Station']['Namen']

当我打印这个我收到错误时

TypeError: list indices must be integers or slices, not str

谁能说出我做错了什么?请使用简单易读的python代码。我是一名学生,开始学习python。

由于

1 个答案:

答案 0 :(得分:0)

stations对象是list个实例:

>>>stations.__class__ 
<class 'list'>

因此,您必须访问赋予list整数索引(或切片,但不适用于此处)的元素,如下所示:

>>>stations[0]
OrderedDict([('Code', 'HT'), ('Type', 'knooppuntIntercitystation'), ('Namen', OrderedDict([('Kort', 'Den Bosch'), ('Middel', "'s-Hertogenbosch"), ('Lang', "'s-Hertogenbosch")])), ('Land', 'NL'), ('Synoniemen', OrderedDict([('Synoniem', ["Hertogenbosch ('s)", 'Den Bosch'])]))])

最后一个对象是Dict个实例(更具体地说是OrderedDict),所以你要找的是:

>>> stations[0]['Namen']
OrderedDict([('Kort', 'Den Bosch'), ('Middel', "'s-Hertogenbosch"), ('Lang', "'s-Hertogenbosch")])

编辑:为了在评论中回答您的上一个问题,您可以执行以下操作:

>>>station_names = []
>>>for station in stations:
>>>     station_names.append(station['Namen'])
>>>#or by comprehension
>>>station_names = [station['Namen'] for station in stations]

无论如何,您将获得:

>>> station_names
[OrderedDict([('Kort', 'Den Bosch'), ('Middel', "'s-Hertogenbosch"), ('Lang', "'s-Hertogenbosch")]), OrderedDict([('Kort', 'Oostvaard'), ('Middel', 'Oostvaarders'), ('Lang', 'Almere Oostvaarders')])]