你能'export'输出python的结果吗?
Can you 'export' output results in python?
假设我计算了五个随机整数列表并将其保存到变量 rands
,如下所示:
rands=[]
for _ in range(5):
rands.append([random.randint(0,10) for _ in range(5)])
然后我打印它:
[[9, 1, 1, 6, 10], [4, 7, 8, 7, 6], [10, 2, 3, 4, 5], [1, 2, 2, 6, 7], [9, 1, 3, 6, 3]]
那我再做一次:
rands2=[]
for _ in range(5):
rands2.append([random.randint(0,10) for _ in range(5)])
并打印出来:
[[4, 8, 4, 8, 7], [5, 3, 5, 8, 3], [2, 0, 6, 3, 0], [1, 3, 2, 2, 6], [9, 10, 3, 10, 0]]
我可以将这两个列表 'export' 保存到 test.py
文件中以便之后导入它们吗?像这样:
rands=[[9, 1, 1, 6, 10], [4, 7, 8, 7, 6], [10, 2, 3, 4, 5], [1, 2, 2, 6, 7], [9, 1, 3, 6, 3]]
rands2=[[4, 8, 4, 8, 7], [5, 3, 5, 8, 3], [2, 0, 6, 3, 0], [1, 3, 2, 2, 6], [9, 10, 3, 10, 0]]
以便我可以在需要这些变量时执行 from test import rands, rands2
?
提前致谢。
您可以创建一个 test.py
,其中包括 rands
和 rands2
,就像您写的那样,然后调用 from test import rands, rands2
在另一个 python 程序中使用这些变量在与 test.py
相同的目录中。如果你想从其他地方导入 test.py
,它必须在 the module search path.
延伸阅读:
https://docs.python.org/3/tutorial/modules.html
对于export
变量,它们必须在python脚本中定义。用Python多写Python一般是个坏主意。您可能想要做的是将这些变量导出到 JSON 文件,然后从该 JSON 文件中读取它们。
进一步阅读 JSON 导出和导入:
https://docs.python.org/3/library/json.html
JSON是一种特殊的数据存储格式,可以方便load/unload用于计算机。您可以将 rands
和 rands2
存储在 Python 字典中,然后将该字典导出到 JSON 文件。
示例:
rands=[[9, 1, 1, 6, 10], [4, 7, 8, 7, 6], [10, 2, 3, 4, 5], [1, 2, 2, 6, 7], [9, 1, 3, 6, 3]]
rands2=[[4, 8, 4, 8, 7], [5, 3, 5, 8, 3], [2, 0, 6, 3, 0], [1, 3, 2, 2, 6], [9, 10, 3, 10, 0]]
import json
# Store the data
with open('test.json', 'w+') as fout:
json.dump({'rands':rands, 'rands2':rands2}, fout)
# Retrieve the data
with open('test.json') as fin:
test = json.load(fin)
rands = test['rands']
rands2 = test['rands2']
为此,您可以使用 Pickle 模块。
- 在您的原始 python 文件中:
import pickle
my_object = [1, 2, 3, 4]
with open('filename', 'wb') as file:
pickle.dump(my_object, file)
- 从新文件 (
test.py
) 中读取此二进制文件以获取对象:
import pickle
with open('filename', 'rb') as file:
my_object = pickle.load(file)
然后您将能够导入此对象:from test import my_object
假设我计算了五个随机整数列表并将其保存到变量 rands
,如下所示:
rands=[]
for _ in range(5):
rands.append([random.randint(0,10) for _ in range(5)])
然后我打印它:
[[9, 1, 1, 6, 10], [4, 7, 8, 7, 6], [10, 2, 3, 4, 5], [1, 2, 2, 6, 7], [9, 1, 3, 6, 3]]
那我再做一次:
rands2=[]
for _ in range(5):
rands2.append([random.randint(0,10) for _ in range(5)])
并打印出来:
[[4, 8, 4, 8, 7], [5, 3, 5, 8, 3], [2, 0, 6, 3, 0], [1, 3, 2, 2, 6], [9, 10, 3, 10, 0]]
我可以将这两个列表 'export' 保存到 test.py
文件中以便之后导入它们吗?像这样:
rands=[[9, 1, 1, 6, 10], [4, 7, 8, 7, 6], [10, 2, 3, 4, 5], [1, 2, 2, 6, 7], [9, 1, 3, 6, 3]]
rands2=[[4, 8, 4, 8, 7], [5, 3, 5, 8, 3], [2, 0, 6, 3, 0], [1, 3, 2, 2, 6], [9, 10, 3, 10, 0]]
以便我可以在需要这些变量时执行 from test import rands, rands2
?
提前致谢。
您可以创建一个 test.py
,其中包括 rands
和 rands2
,就像您写的那样,然后调用 from test import rands, rands2
在另一个 python 程序中使用这些变量在与 test.py
相同的目录中。如果你想从其他地方导入 test.py
,它必须在 the module search path.
延伸阅读: https://docs.python.org/3/tutorial/modules.html
对于export
变量,它们必须在python脚本中定义。用Python多写Python一般是个坏主意。您可能想要做的是将这些变量导出到 JSON 文件,然后从该 JSON 文件中读取它们。
进一步阅读 JSON 导出和导入: https://docs.python.org/3/library/json.html
JSON是一种特殊的数据存储格式,可以方便load/unload用于计算机。您可以将 rands
和 rands2
存储在 Python 字典中,然后将该字典导出到 JSON 文件。
示例:
rands=[[9, 1, 1, 6, 10], [4, 7, 8, 7, 6], [10, 2, 3, 4, 5], [1, 2, 2, 6, 7], [9, 1, 3, 6, 3]]
rands2=[[4, 8, 4, 8, 7], [5, 3, 5, 8, 3], [2, 0, 6, 3, 0], [1, 3, 2, 2, 6], [9, 10, 3, 10, 0]]
import json
# Store the data
with open('test.json', 'w+') as fout:
json.dump({'rands':rands, 'rands2':rands2}, fout)
# Retrieve the data
with open('test.json') as fin:
test = json.load(fin)
rands = test['rands']
rands2 = test['rands2']
为此,您可以使用 Pickle 模块。
- 在您的原始 python 文件中:
import pickle
my_object = [1, 2, 3, 4]
with open('filename', 'wb') as file:
pickle.dump(my_object, file)
- 从新文件 (
test.py
) 中读取此二进制文件以获取对象:
import pickle
with open('filename', 'rb') as file:
my_object = pickle.load(file)
然后您将能够导入此对象:from test import my_object