0

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>

我读取 xmlfile 的 python 代码:

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。

谢谢

4

1 回答 1

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')])]
于 2017-10-09T16:58:19.377 回答