473,503 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python dictionaries from .txt

2 New Member
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!
Nov 6 '09 #1
3 2733
bvdet
2,851 Recognized Expert Moderator Specialist
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. >>> 
Nov 6 '09 #2
timinator18
2 New Member
i love you. thanks a bunch!!!!!
Nov 6 '09 #3
bvdet
2,851 Recognized Expert Moderator Specialist
@timinator18
Let's not get carried away! :)
Nov 7 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
2816
by: Don Bruder | last post by:
A week or two ago, I asked here about porting Python to C. Got some good answers (Note 1) but now I've got another question. Actually, more a request for clarification of a topic that both the...
0
1351
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
44
2475
by: Iwan van der Kleyn | last post by:
Please ignore if you are allergic to ramblings :-) Despite a puritan streak I've always tried to refrain from language wars or syntax bickering; call it enforced pragmatism. That's the main...
4
2414
by: Tim Henderson | last post by:
Hi The question why are there no sorted dictionaries in python, seems to pop up with unseeming regularity. That question in itself in nonsensical sense dictionaries are hash-maps, however should...
1
1473
by: François Pinard | last post by:
Hi, people. I noticed today that dictionaries seem to support `==' comparison. (Retrospectively, it is strange that I never needed it before! :-) Yet, before relying on this, I seeked for...
0
273
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 419 open ( +3) / 3410 closed ( +2) / 3829 total ( +5) Bugs : 910 open (+12) / 6185 closed ( +5) / 7095 total (+17) RFE : 235 open...
0
1644
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 420 open ( +4) / 3410 closed ( +2) / 3830 total ( +6) Bugs : 915 open (+17) / 6186 closed ( +6) / 7101 total (+23) RFE : 235 open...
5
1323
by: Larry Bates | last post by:
Peter Anderson wrote: Names are pointers in Python that point to values in memory. Names are "bound" to these values with assignment. Names are NOT buckets where you put values as is the case...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.