【发布时间】:2023-01-19 18:16:43
【问题描述】:
我将数组存储到文件中:
file = open("file1.txt", "w+")
# Saving the 2D array in a text file
content = array2d
file.write(str(content))
file.close()
现在我必须在文件中使用看起来像这样的数组(这只是缩短):
[[[ 253 122]
[ 253 121]
[ 253 121]
...
[1027 119]
[1027 120]
[1028 120]]
[[ 252 122]
[ 253 122]
[ 253 122]
...
[1067 573]
[1067 573]
[1067 573]]]
我必须打开这个文件并将数组存储在新文件中,以便在保存之前访问所有整数元素。
我试过:
text_file = open("file1.txt", "r")
data = []
data = text_file.read()
text_file.close()
print(data[0])
作为第一个元素数据 [0] 给我 [ 它应该是 253。
【问题讨论】:
-
文件的确切格式是什么?
-
这回答了你的问题了吗? best way to preserve numpy arrays on disk