可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
Just import it
import script
print script.airportCode
or, better
from script 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).
回答2:
Assuming your import myDict
works, you need to do the following:
from myDict import airportCode
回答3:
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.
回答4:
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.
回答5:
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).
回答6:
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.
回答7:
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.