I know I can include Python code from a common file using import MyModuleName
- but how do I go about importing just a dict?
我知道我可以使用import MyModuleName从公共文件中包含Python代码 - 但是我如何才能导入一个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.
我试图解决的问题是我有一个需要在可编辑位置的文件中的字典,而实际的脚本在另一个文件中。 dict也可以由非程序员手工编辑。
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?
如何从script.py中访问airportCode dict?
7 个解决方案
#1
33
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).
只是要小心将两个脚本放在同一个目录上(或者创建一个python包,一个带有__init__.py文件的子目录;或者将路径放到PYTHONPATH上的script.py;但这些是“高级选项”,只需将它放在同一个目录,它会没事的。
#2
9
Assuming your import myDict
works, you need to do the following:
假设您的导入myDict有效,您需要执行以下操作:
from myDict import airportCode
#3
2
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:
当您在python中执行导入时,您实际上只是将名称拉入当前命名空间。这些名称所引用的内容并不重要:
from myDict import airportCode
Will work regardless of whether airportCode
is a function, class or just a field as in your case.
无论airportCode是函数,类还是仅仅是一个字段,都可以工作。
#4
1
Well, it doesn't need to be a .py
file. You could just do:
好吧,它不需要是.py文件。你可以这样做:
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.
您可能想要查看的另一个模块是用于导入CSV文件的csv。然后,您的用户可以使用电子表格对其进行编辑,您无需教他们Python语法。
#5
1
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.
如果您的dict必须由非程序员手动编辑,那么使用CSV文件可能更有意义。然后你的编辑器甚至可以使用Excel。
So you can use:
所以你可以使用:
import csv
csvfile = csv.reader(open("airports.csv"))
airportCode = dict(csvfile)
to read a CSV file like
阅读像。的CSV文件
"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
0
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.
如果应该从不同目录中的Python模块访问myDict,则必须提供__init__.py模块。
For more Information about this topic have a look at the module chapter of the Python documentation.
有关此主题的更多信息,请查看Python文档的模块章节。
#7
0
Use csv. Stick import csv
with the rest of your module imports, and then you can do as follows:
使用csv。使用其余的模块导入粘贴导入csv,然后您可以执行以下操作:
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:
创建csv文件:
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.
这将创建一个csv文件,机场和IATA TLA分为两列,机场和iatacode作为标题。
You can also skip the dicts and just have strings by using Reader and Writer rather than DictReader and DictWriter.
你也可以跳过dicts,只使用Reader和Writer而不是DictReader和DictWriter。
By default, the csv module produces excel-style csv, but you can set whatever dialect you like as a kwarg.
默认情况下,csv模块会生成excel样式的csv,但您可以将任何您喜欢的方言设置为kwarg。
#1
33
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).
只是要小心将两个脚本放在同一个目录上(或者创建一个python包,一个带有__init__.py文件的子目录;或者将路径放到PYTHONPATH上的script.py;但这些是“高级选项”,只需将它放在同一个目录,它会没事的。
#2
9
Assuming your import myDict
works, you need to do the following:
假设您的导入myDict有效,您需要执行以下操作:
from myDict import airportCode
#3
2
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:
当您在python中执行导入时,您实际上只是将名称拉入当前命名空间。这些名称所引用的内容并不重要:
from myDict import airportCode
Will work regardless of whether airportCode
is a function, class or just a field as in your case.
无论airportCode是函数,类还是仅仅是一个字段,都可以工作。
#4
1
Well, it doesn't need to be a .py
file. You could just do:
好吧,它不需要是.py文件。你可以这样做:
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.
您可能想要查看的另一个模块是用于导入CSV文件的csv。然后,您的用户可以使用电子表格对其进行编辑,您无需教他们Python语法。
#5
1
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.
如果您的dict必须由非程序员手动编辑,那么使用CSV文件可能更有意义。然后你的编辑器甚至可以使用Excel。
So you can use:
所以你可以使用:
import csv
csvfile = csv.reader(open("airports.csv"))
airportCode = dict(csvfile)
to read a CSV file like
阅读像。的CSV文件
"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
0
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.
如果应该从不同目录中的Python模块访问myDict,则必须提供__init__.py模块。
For more Information about this topic have a look at the module chapter of the Python documentation.
有关此主题的更多信息,请查看Python文档的模块章节。
#7
0
Use csv. Stick import csv
with the rest of your module imports, and then you can do as follows:
使用csv。使用其余的模块导入粘贴导入csv,然后您可以执行以下操作:
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:
创建csv文件:
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.
这将创建一个csv文件,机场和IATA TLA分为两列,机场和iatacode作为标题。
You can also skip the dicts and just have strings by using Reader and Writer rather than DictReader and DictWriter.
你也可以跳过dicts,只使用Reader和Writer而不是DictReader和DictWriter。
By default, the csv module produces excel-style csv, but you can set whatever dialect you like as a kwarg.
默认情况下,csv模块会生成excel样式的csv,但您可以将任何您喜欢的方言设置为kwarg。