Connecting Tech Pros Worldwide Help | Site Map

Python dictionaries from .txt

Newbie
 
Join Date: Nov 2009
Posts: 2
#1: 2 Weeks Ago
Function Name Description
load_airports(reader)
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:YEG:EDMONTON:US
FREDERICTON:YFC:FREDERICTON
HALIFAX INTERNATIONAL:YHZ:HALIFAX:INT
MONTREAL INTERNATIONAL MIRABEL:YMX:MONTREAL:INT
OLD CROW:YOC:OLD CROW
WINDSOR:YQG:WINDSOR
REGINA INTERNATIONAL:YQR:REGINA:US
THUNDER BAY:YQT:THUNDER BAY
VANCOUVER INTERNATIONAL:YVR:VANCOUVER:INT
MONT JOLI:YYY:MONT JOLI
LESTER B PEARSON INTERNATIONAL:YYZ: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's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#2: 2 Weeks Ago

re: Python dictionaries from .txt


Following is an example of reading the file into 2 dictionaries. You should be able to modify this to suit your purposes.
Expand|Select|Wrap|Line Numbers
  1. f = open("small_airports.txt")
  2. dd1 = {}
  3. dd2 = {}
  4. for line in f:
  5.     lineList = line.strip().split(":")
  6.     # Has US or INT flights
  7.     if len(lineList) > 3:
  8.         dd2[lineList[1]] = lineList[0], lineList[3]
  9.     else:
  10.         dd1[lineList[1]] = lineList[0]
  11.  
  12. f.close()
  13.  
  14. print dd1
  15. print dd2
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> {'YFC': 'FREDERICTON', 'YDF': 'DEER LAKE', 'YOC': 'OLD CROW', 'YQG': 'WINDSOR', 'YYY': 'MONT JOLI', 'YQT': 'THUNDER BAY'}
  2. {'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')}
  3. >>> 
Newbie
 
Join Date: Nov 2009
Posts: 2
#3: 2 Weeks Ago

re: Python dictionaries from .txt


i love you. thanks a bunch!!!!!
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#4: 2 Weeks Ago

re: Python dictionaries from .txt


Quote:

Originally Posted by timinator18 View Post

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

Let's not get carried away! :)
Reply