gpt4 book ai didi

python - 将 python 3 中的大列表保存到二进制文件

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:17 27 4
gpt4 key购买 nike

我在 Python 3 中有一个很大的列表,如下所示:

array = [[262,231],[123,222],[237272,292]...]

我想将其保存在 python 中的二进制文件中我试过这个:

output = open("pratica3.dat", "wb")
inbytes = bytes(array)
output.write(inbytes)

但这会产生错误:类型错误:“列表”对象无法解释为整数

如果我尝试遍历列表并保存,这会给我另一个超出范围 256 的错误

最佳答案

我认为您正在寻找pickle:

import pickle

pickle.dump(array, output) # converts array to binary and writes to output

array = pickle.load(output) # Reads the binary and converts back to list

关于python - 将 python 3 中的大列表保存到二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36024225/

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