0

I'm trying to make the program read the CSV file ("airports.csv") and save it into the dictionary, where by I can enter the key in and this will call the airport objects associated with this key.

When I try to print the whole list to make sure it is working it does print the key but prints{'GKA: <__main__.Airport object as 0x02E92570>}'

As the question is based around using the dictionary objects and not lists, could you explain where I am going wrong. I may have mixed some of it up and went the wrong way about it as I have always made dictionaries with lists and not objects.

I have attached the CSV file and the tutorial.

If anybody could show me how to properly call a object in a dictionary. Thanks

    import csv
    class Airport:

        def __init__(self, idx=-1, airportname='', cityname='', countryname='', code3='',
        code4='',lat=0,long=0, altitude=0,timezone='',DST='',Tz=''):

            self.idx=idx
            self.airportname=airportname
            self.cityname=cityname
            self.countryname=countryname
            self.code3=code3
            self.code4=code4
            self.lat=lat
            self.long=long
            self.lat=lat
            self.altitude=altitude
            self.timezone=timezone
            self.DST=DST
            self.Tz=Tz

        def dictairportChosen(self,filename):

            self.__airportDict={}

            f=open(filename, encoding="utf8")
            csvreader = csv.reader(f)

            for idx, airportname, cityname, countryname, code3, code4, lat, long, altitude, timezone, DST, TZ,  in csvreader:
                airport=Airport(idx, airportname, cityname, countryname, code3, code4, lat, long, altitude, timezone, DST, TZ)
                self.__airportDict[code3]=airport
                print(self.__airportDict)


    aAirportChosen=Airport()
    aAirportChosen.dictairportChosen("airports.csv")

The this is a snippet of the CSV file:

    1,"Goroka","Goroka","Papua New Guinea","GKA","AYGA",-6.081689,145.391881,5282,10,"U","Pacific/Port_Moresby"
    2,"Madang","Madang","Papua New Guinea","MAG","AYMD",-5.207083,145.7887,20,10,"U","Pacific/Port_Moresby"
    3,"Mount Hagen","Mount Hagen","Papua New Guinea","HGU","AYMH",-5.826789,144.295861,5388,10,"U","Pacific/Port_Moresby"
    4,"Nadzab","Nadzab","Papua New Guinea","LAE","AYNZ",-6.569828,146.726242,239,10,"U","Pacific/Port_Moresby"
    5,"Port Moresby Jacksons Intl","Port Moresby","Papua New Guinea","POM","AYPY",-9.443383,147.22005,146,10,"U","Pacific/Port_Moresby"
    6,"Wewak Intl","Wewak","Papua New Guinea","WWK","AYWK",-3.583828,143.669186,19,10,"U","Pacific/Port_Moresby"
    7,"Narsarsuaq","Narssarssuaq","Greenland","UAK","BGBW",61.160517,-45.425978,112,-3,"E","America/Godthab"
    8,"Nuuk","Godthaab","Greenland","GOH","BGGH",64.190922,-51.678064,283,-3,"E","America/Godthab"

The question of the actual tutorial is: 2. Write code to read from the csv and create a dictionary of airport objects Replace the lists in the dictionary with airport objects. You should end up with a dictionary that has the airport code as a key and an airport object as the lookup value allowing the code below to run:

airportLookupDict={‘DUB’, airport(….),‘LHR’, airport(….)}
myairport=airportLookupDict.get(‘DUB’)
print(myairport.name)
outputs:
DUBLIN
daphshez
  • 9,272
  • 11
  • 47
  • 65
Albi
  • 33
  • 5
  • 2
    Why do you think there's something wrong? You have a dictionary where the key is the airport's code and the value is the `Airport` instance - what were you expecting? – jonrsharpe May 05 '15 at 15:09
  • That was from when i was attempting to have the dictionary in it's own class. Changed it there and it worked. thanks – Albi May 05 '15 at 15:16

2 Answers2

0

Untested, but you could try

self.__airportDict[code3]=airport.__dict__

from Python dictionary from an object's fields

Community
  • 1
  • 1
Sam Cohen-Devries
  • 2,025
  • 2
  • 18
  • 35
0

There is nothing wrong with your code. It works as expected in the question. If you want to access individual elements of the list just add the entity name to the list as follows:

    for keys, values in self.__airportDict.items():
        print keys
        print values.airportname
Jerry Ajay
  • 1,084
  • 11
  • 26