0

Part of the XML file:

<?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>

My python code to read 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()

With Program() I'll get a list with the type and code tag out of the xml file.

I'm trying now to figure out how to get the lang tag out of it and make a similair list like the one above but with only Code and Lang tags

I've made a new variable:

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

when I print this i'm getting an error

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

Can anyone say what i'm doing wrong? Please use simple and readable python code. I'm a student started learning python.

Thanks

Proxi Henn
  • 35
  • 1
  • 1
  • 6

1 Answers1

0

The stations object is a list instance:

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

Therefore, you have to access to elements giving to list an integer index(or slices, but does not applies here), like this:

>>>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'])]))])

This last object is a Dict instance(more specifically an OrderedDict), so what you are looking for is:

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

Edit: In order to answer your last question in comments, you could do the following:

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

In any case you will obtain:

>>> station_names
[OrderedDict([('Kort', 'Den Bosch'), ('Middel', "'s-Hertogenbosch"), ('Lang', "'s-Hertogenbosch")]), OrderedDict([('Kort', 'Oostvaard'), ('Middel', 'Oostvaarders'), ('Lang', 'Almere Oostvaarders')])]
Rafael Aguilar
  • 3,084
  • 1
  • 25
  • 31
  • Great explanation sir. – Proxi Henn Oct 09 '17 at 17:09
  • 1
    So i've made this now: insideName = stations[0]['Name'] >>> print(insideName['Lang'] I'm getting the correct answer for one station, but how do i print out all the values of dictionary Namen? So without putting the [0] because that is a specific station. I need them all at once. – Proxi Henn Oct 09 '17 at 17:15