将多维列表保存在文件中 Python

Saving a multi-dimensional list in a file Python

我以这种方式创建了这个列表 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))

我需要将结果保存在文本文件中。我试过 numpy.savetxt、pickle.dump 和 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()

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.]

泡菜

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])

我无法从所有这些保存的文件中取回原始文件 array/list。当我尝试使用这些加载列表中的任何一个时,出现某种错误:

np.loadtxt

could not convert string to float: '[0.]'

打开()

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

作为列表的 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]),

我怎样才能说出我的多维列表,以便我可以准确地回到我开始的地方?

额外:mass_array1 文件

https://drive.google.com/file/d/1Kdmv1fcbDelEzGmi4BOE4HjUbM7Cg23b/view?usp=sharing

这就是我将其导入 python:

的方式

您需要先将文件解压到一个文件夹中。

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

您最好将其保存为 .csv(以逗号分隔的文件,以便您轻松上传或获取。

使用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

更新:使用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

首先,了解您创建的内容:

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]

这是一个列表,主要包含 0 和一个或多个一维数组。

csv 文件格式适用于“table”,许多行都具有相同的列数。

savetxt写一个数组,最好是2d的,但是1d的也行。但是你给了它一个清单。所以它必须先制作一个数组:

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)

%s保存的结果是:

In [99]: cat bhs.txt
0
[900.04634682  67.58574156 364.69588687 868.10145473]
0
0
0

该数组元素被写为 str 显示。使用 csv 工具很难(尽管并非不可能)加载这样的文件。这不是正确的 csv 文件。

pickle 几乎可以处理任何 python 对象,包括各种东西的列表:

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]

Out[98]中列表的数组版本也可以保存为数组(嵌入pickling):

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)

我怀疑您是否真的想要或应该创建这样的数组列表。在任何情况下,请确保您在尝试以随机选择的格式保存之前了解您创建的内容。