我有一个共享文件夹,其中包含我希望我的程序参考的文本文件,就好像它是.py文件一样。例如,我的.txt文件中有一个我在程序中引用的字典。我如何从文本文件导入字典?我是否一行一行阅读?或者有没有一种方法可以让python认为它是一个.py文件。如何在Python 2.7中将文本(.txt)文件读取为.py文件?
这里是类似我的.txt文件的样本:
#There are some comments here and there
#lists of lists
equipment = [['equip1','hw','1122','3344'],['equip2','hp','1133','7777'],['equip3','ht','3333','2745']]
#dictionaries
carts = {'001':'Rev23', '002':'Rev11','003':'Rev7'}
#regular lists
stations = ("1", "2", "3", "4", "11", "Other")
相关:[动态导入Python模块](http://stackoverflow.com/q/3799545/4279) – jfs