Python dictionaries from .txt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • timinator18
    New Member
    • Nov 2009
    • 2

    Python dictionaries from .txt

    Function Name Description
    load_airports(r eader)
    Given an open reader that contains airport data in the format described above this table, return a tuple that contains two dictionaries and a list.

    The first dictionary should contain three-letter airport codes (of type str) as keys and tuples with two elements as values. Each tuple should contain the city the airport is associated with (str) as the first value and a tuple containing its latitude (float) and longitude (float) as the second value.

    The latitude and longitude tuple should be obtained by providing the name of the airport followed by the word CANADA to the geocode function of the maps module. If the maps module cannot provide the latitude and longitude of the airport, the airport should not be placed in this dictionary.

    The second dictionary should also use three-letter airport codes as keys. An airport should only be represented in this dictionary if it has US or international flights. If an airport has US flights (but no international flights), its value in the dictionary should be the str "US". If it has international flights, its value should be the str "INT". Again, if the maps module cannot provide the latitude and longitude of the airport, the airport should not be placed in this dictionary.

    The list should contain the three-letter codes of all airports that the geocode function (which uses Google's geocoding service) cannot locate.

    airport info is located here. its supposed to be part of a .txt file called small_airports. txt

    DEER LAKE:YDF:DEER LAKE
    EDMONTON INTERNATIONAL:Y EG:EDMONTON:US
    FREDERICTON:YFC :FREDERICTON
    HALIFAX INTERNATIONAL:Y HZ:HALIFAX:INT
    MONTREAL INTERNATIONAL MIRABEL:YMX:MON TREAL:INT
    OLD CROW:YOC:OLD CROW
    WINDSOR:YQG:WIN DSOR
    REGINA INTERNATIONAL:Y QR:REGINA:US
    THUNDER BAY:YQT:THUNDER BAY
    VANCOUVER INTERNATIONAL:Y VR:VANCOUVER:IN T
    MONT JOLI:YYY:MONT JOLI
    LESTER B PEARSON INTERNATIONAL:Y YZ:TORONTO:INT

    My question is the following: How do i take this .txt file, split the parts, and put these separate elements into the appropriate lists and dictionaries, etc. The latitude and Longitude tuple can't be completed unless you have the program for it, so dont worry about that part. Thanks a bunch!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Following is an example of reading the file into 2 dictionaries. You should be able to modify this to suit your purposes.
    Code:
    f = open("small_airports.txt")
    dd1 = {}
    dd2 = {}
    for line in f:
        lineList = line.strip().split(":")
        # Has US or INT flights
        if len(lineList) > 3:
            dd2[lineList[1]] = lineList[0], lineList[3]
        else:
            dd1[lineList[1]] = lineList[0]
    
    f.close()
    
    print dd1
    print dd2
    Output:
    Code:
    >>> {'YFC': 'FREDERICTON', 'YDF': 'DEER LAKE', 'YOC': 'OLD CROW', 'YQG': 'WINDSOR', 'YYY': 'MONT JOLI', 'YQT': 'THUNDER BAY'}
    {'YVR': ('VANCOUVER INTERNATIONAL', 'INT'), 'YMX': ('MONTREAL INTERNATIONAL MIRABEL', 'INT'), 'YEG': ('EDMONTON INTERNATIONAL', 'US'), 'YYZ': ('LESTER B PEARSON INTERNATIONAL', 'INT'), 'YHZ': ('HALIFAX INTERNATIONAL', 'INT'), 'YQR': ('REGINA INTERNATIONAL', 'US')}
    >>>

    Comment

    • timinator18
      New Member
      • Nov 2009
      • 2

      #3
      i love you. thanks a bunch!!!!!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by timinator18
        i love you. thanks a bunch!!!!!
        Let's not get carried away! :)

        Comment

        Working...