作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个包含 16 个元素的列表,每个元素还有 500 个元素长。我想将其写入一个 txt 文件,这样我就不必再从模拟中创建列表。我怎样才能做到这一点,然后再次访问该列表?
最佳答案
Pickle 可以,但缺点是它是 Python 特定的二进制格式。另存为 JSON 以便于阅读和在其他应用程序中重复使用:
import json
LoL = [ range(5), list("ABCDE"), range(5) ]
with open('Jfile.txt','w') as myfile:
json.dump(LoL,myfile)
文件现在包含:
[[0, 1, 2, 3, 4], ["A", "B", "C", "D", "E"], [0, 1, 2, 3, 4]]
稍后取回:
with open('Jfile.txt','r') as infile:
newList = json.load(infile)
print newList
关于python - 如何将列表列表写入 txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19460457/
我是一名优秀的程序员,十分优秀!