Pranab
Pranab

Reputation: 2797

How to import or include data structures (e.g. a dict) into a Python file from a separate file

I know I can include Python code from a common file using import MyModuleName - but how do I go about importing just a dict?

The problem I'm trying to solve is I have a dict that needs to be in a file in an editable location, while the actual script is in another file. The dict might also be edited by hand, by a non-programmer.

script.py

airportName = 'BRISTOL'
myAirportCode = airportCode[airportName]

myDict.py

airportCode = {'ABERDEEN': 'ABZ', 'BELFAST INTERNATIONAL': 'BFS', 'BIRMINGHAM INTERNATIONAL': 'BHX', 'BIRMINGHAM INTL': 'BHX', 'BOURNMOUTH': 'BOH', 'BRISTOL': 'BRS'}

How do I access the airportCode dict from within script.py?

Upvotes: 55

Views: 156610

Answers (7)

Khelben
Khelben

Reputation: 6461

Just import it

import myDict
print myDict.airportCode

or, better

from myDict import airportCode
print airportCode

Just be careful to put both scripts on the same directory (or make a python package, a subdir with __init__.py file; or put the path to script.py on the PYTHONPATH; but these are "advanced options", just put it on the same directory and it'll be fine).

Upvotes: 82

user106514
user106514

Reputation: 159

Use csv. Stick import csv with the rest of your module imports, and then you can do as follows:

f = open('somefile.csv')
reader = csv.DictReader(f, (airport, iatacode))
for row in reader:
   print row

which should give you a list of dictionaries:

airport | iatacode
__________________
Aberdeen| ABZ

to create the csv file:

f = open('somefile.csv', 'w')
writer = csv.DictWriter(f, (airport, iatacode))
for row in airportcode:
   writer.writerow()
f.close()

which will create a csv file with airports and IATA TLAs in two columns with airport and iatacode as the headers.

You can also skip the dicts and just have strings by using Reader and Writer rather than DictReader and DictWriter.

By default, the csv module produces excel-style csv, but you can set whatever dialect you like as a kwarg.

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336428

If your dict has to be hand-editable by a non-programmer, perhaps it might make more sense using a CSV file for this. Then you editor can even use Excel.

So you can use:

import csv
csvfile = csv.reader(open("airports.csv"))
airportCode = dict(csvfile)

to read a CSV file like

"ABERDEEN","ABZ"
"BELFAST INTERNATIONAL","BFS"
"BIRMINGHAM INTERNATIONAL","BHX"
"BIRMINGHAM INTL","BHX"
"BOURNMOUTH","BOH"
"BRISTOL","BRS"

Careful: If an airport were in that list twice, the last occurrence would silently "overwrite" any previous one(s).

Upvotes: 1

Tendayi Mawushe
Tendayi Mawushe

Reputation: 26138

When you perform an import in python you are really just pulling in names into your current namespace. It does not really matter what those names refer to so:

from myDict import airportCode

Will work regardless of whether airportCode is a function, class or just a field as in your case.

Upvotes: 2

pi.
pi.

Reputation: 21582

from myDict import airportCode
airportNode = 'BRISTOL'
myAirportCode = airportCode[airportName]

If myDict should get accessed from a Python module in a different directory, you have to provide a __init__.py module.

For more Information about this topic have a look at the module chapter of the Python documentation.

Upvotes: 0

Mike DeSimone
Mike DeSimone

Reputation: 42825

Well, it doesn't need to be a .py file. You could just do:

eval(open("myDict").read())

It's a gaping security hole, though.

Another module you might want to look at is csv for importing CSV files. Then your users could edit it with a spreadsheet and you don't have to teach them Python syntax.

Upvotes: 2

SilentGhost
SilentGhost

Reputation: 319881

Assuming your import myDict works, you need to do the following:

from myDict import airportCode

Upvotes: 17

Related Questions