[英]Saving a multi-dimensional list in a file Python
I have this list bhs created this way:我以这种方式创建了这个列表 bhs:
#Alotting Black Holes at z=6
bhs=[0]*1000
for i in tqdm(range(0,1000),position=0, leave=True):
if len(mass_array1[i])!=0:
bhs[i]=np.zeros(len(mass_array1[i]))
else:
bhs[i]=np.zeros(1)
for j in range (len(mass_array1[i])):
bhs[i][j]=np.random.lognormal(np.log(MbhthShimasaku(mass_array1[i],6)[j]),np.log(5))
I need to save the result in a text file.我需要将结果保存在文本文件中。 I have tried numpy.savetxt, pickle.dump and open():
我试过 numpy.savetxt、pickle.dump 和 open():
open()打开()
with open("bhs.txt", 'w') as file:
for row in bhs:
s = " ".join(map(str, row))
file.write(s+'\n')
#Result .txt file:
0.0
0.0
0.0
0.0
1937651.7861915156 246221.20328840986 226756.87389065413
0.0
0.0
numpy.savetxt() numpy.savetxt()
bhs=np.array(bhs)
np.savetxt('bhs.txt',bhs,fmt='%s')
#Result .txt file:
[0.]
[0.]
[0.]
[0.]
[26447480.89508711 1097038.92200952 971383.67441455]
[0.]
[0.]
[0.]
[0.]
[0.]
pickle泡菜
bhs.append(bhs)
tupleA=tuple(bhs)
filename = 'bhs.p'
with open(filename, 'wb') as filehandler:
pickle.dump(tupleA, filehandler)
#Result .p file
array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([1937651.78619152, 246221.20328841, 226756.87389065])
I am unable to get back the original array/list from all these saved files.我无法从所有这些保存的文件中取回原始数组/列表。 When I try to use any of these loaded lists, I get some kind of error:
当我尝试使用这些加载列表中的任何一个时,我会收到某种错误:
np.loadtxt np.loadtxt
could not convert string to float: '[0.]'
open()打开()
my_file = open("bhs.txt", "r")
content = my_file.read()
content_list = content.split(",")
my_file.close()
print(content_list)
[0.]\n[0.]\n[26447480.89508711 1097038.92200952 971383.67441455]\n[0.]\n[0.]\n[0.]\n[0.]\n[0.]\n[0.]\n[0.]\n[0.]\n
Sample of bhs as a list作为列表的 bhs 样本
array([1461403.98258597]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([26447480.89508711, 1097038.92200952, 971383.67441455]),
How can I say my multidimensional list so that I can get back exactly what I started with?我怎样才能说出我的多维列表,以便我可以准确地回到我开始时的内容?
Extra: mass_array1 file额外:mass_array1 文件
https://drive.google.com/file/d/1Kdmv1fcbDelEzGmi4BOE4HjUbM7Cg23b/view?usp=sharing https://drive.google.com/file/d/1Kdmv1fcbDelEzGmi4BOE4HjUbM7Cg23b/view?usp=sharing
And this is how I import it into python:这就是我将它导入 python 的方式:
You need to unzip the file into a folder first.您需要先将文件解压缩到一个文件夹中。
dirlist=["bh2e10"]
import time
mass_array1=[0]*1000
#print(mass_array)
#read all the files
for i,X in enumerate(dirlist):
exec('filelist=glob.glob("%s/test*.dat")'%(X))
#exec("mass_array%s=[]"%X)
initial_mass=[]
for j,Y in tqdm(enumerate(filelist),position=0, leave=True, total=1000):
Y=Y.replace(os.sep, '/')
#Z=int(Y[10:13])
Z=int(re.findall("\d+", Y)[2])
#print(Z)
mass_array1[Z]=[]
#print('i=',Z,end="\r")
#print('i=',Z,end="\r")
exec("initial_partial=np.loadtxt('%s',max_rows=1)"%(Y))
exec("initial_mass=np.append(initial_mass,initial_partial)")
exec("mass_partial=np.loadtxt('%s',skiprows=1)"%(Y))
mass_array1[Z]=np.append(mass_partial,mass_array1[Z])
#mass_array1[Z]=mass_partial
Use csv
module使用
csv
模块
import numpy as np
import csv
bhs = [[0.], [0.], [0.], [0.], [26447480.89508711, 1097038.92200952, 971383.67441455], [0.], [0.], [0.], [0.], [0.]]
# write to csv
with open("bhs.txt", mode="w", newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(bhs)
# read from csv
with open("bhs.txt", mode="r") as csvfile:
reader = csv.reader(csvfile)
bhs1 = [np.array(row, dtype=np.float).tolist() for row in reader]
>>> bhs == bhs1
True
Update : use joblib
更新:使用
joblib
import joblib
bhs = [[0.], [0.], [0.], [0.], [26447480.89508711, 1097038.92200952, 971383.67441455], [0.], [0.], [0.], [0.], [0.]]
joblib.dump(bhs, "bhs.txt")
bhs1 = joblib.load("bhs.txt")
>>> bhs == bhs1
True
First, understand what you created:首先,了解您创建的内容:
In [94]: bhs = [0]*5
In [95]: bhs[1]=np.random.rand(4)*1000
In [96]: bhs
Out[96]: [0, array([900.04634682, 67.58574156, 364.69588687, 868.10145473]), 0, 0, 0]
It's a list, with mostly 0s, and one or more 1d arrays.这是一个列表,大部分为 0,以及一个或多个 1d arrays。
The csv
file format is intended for a "table", many rows all with the same number of columns. csv
文件格式用于“表格”,许多行都具有相同的列数。
savetxt
writes an array, preferably 2d, but it can work with 1d. savetxt
写入一个数组,最好是 2d,但它可以与 1d 一起使用。 But you gave it a list.但是你给了它一个清单。 So it had to make an array first:
所以它必须先制作一个数组:
In [98]: np.array(bhs)
<ipython-input-98-fe2575327968>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
np.array(bhs)
Out[98]:
array([0, array([900.04634682, 67.58574156, 364.69588687, 868.10145473]),
0, 0, 0], dtype=object)
The result of saving that with %s
is:用
%s
保存的结果是:
In [99]: cat bhs.txt
0
[900.04634682 67.58574156 364.69588687 868.10145473]
0
0
0
That array element was been written as the str
display.该数组元素被写入
str
显示。 Such a file is hard, though not impossible, to load with a csv
tool.使用
csv
工具很难加载这样的文件,尽管并非不可能。 It is not a proper csv file.它不是正确的 csv 文件。
pickle
can handle almost any python object, including a list of various stuff: pickle
几乎可以处理任何 python object,包括各种东西的列表:
In [102]: with open('bhs.p','wb') as f:
...: pickle.dump(bhs, f)
...:
In [105]: with open('bhs.p','rb') as f:
...: new=pickle.load(f)
...:
...:
In [106]: new
Out[106]: [0, array([900.04634682, 67.58574156, 364.69588687, 868.10145473]), 0, 0, 0]
The array version of the list in Out[98] can also be saved as an array (with embedded pickling): Out[98] 中列表的数组版本也可以保存为数组(带有嵌入式酸洗):
In [110]: np.save('foo.npy',_98)
In [111]: np.load('foo.npy', allow_pickle=True)
Out[111]:
array([0, array([900.04634682, 67.58574156, 364.69588687, 868.10145473]),
0, 0, 0], dtype=object)
I question whether you really want or should be creating a list of arrays like this.我怀疑你是否真的想要或应该像这样创建一个 arrays 列表。 In any, case make sure you understand what you've created before trying to save it randomly selected formats.
无论如何,在尝试保存随机选择的格式之前,请确保您了解您创建的内容。
You have better to save it in a.csv (comma separe file so you can easly upload or take it.您最好将其保存在 a.csv (逗号分隔文件,以便您可以轻松上传或获取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.