繁体   English   中英

在文件中保存多维列表 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])

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

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模块

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,以及一个或多个 1d arrays。

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

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

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)

我怀疑你是否真的想要或应该像这样创建一个 arrays 列表。 无论如何,在尝试保存随机选择的格式之前,请确保您了解您创建的内容。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM