gpt4 book ai didi

python - 如何将列表列表写入 txt 文件?

转载 作者:太空狗 更新时间:2023-10-30 01:44:48 26 4
gpt4 key购买 nike

我有一个包含 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com