[英]How do I load a matrix from a .txt file in python?
我在 txt 文件中有一个看起来像这样的矩阵:
[[0.26263508 0.89992943 0.62171512 0.20750958 0.21195397 0.97217826
0.61573457 0.05643889]
[0.33188798 0.32016444 0.92051048 0.75572024 0.20247452 0.37400282
0.10935296 0.63343081]
[0.87017165 0.7283508 0.80314653 0.80094718 0.74024014 0.16330332
0.76579785 0.75177055]
[0.2629302 0.59727507 0.60866212 0.29746334 0.54587234 0.43876005
0.75007362 0.89742691]
[0.05300406 0.83342629 0.19291691 0.83372532 0.98122163 0.7815009
0.59635085 0.9700382 ]
[0.69259902 0.42779514 0.04766533 0.62205107 0.71423376 0.85045446
0.31985818 0.15338853]
[0.26947509 0.41946874 0.87206754 0.35849082 0.94756447 0.59001803
0.41028535 0.85643487]
[0.87299386 0.70986812 0.87212445 0.30309828 0.31214338 0.33387522
0.52875374 0.75712628]
[0.51605143 0.64374971 0.37821579 0.77055732 0.12504581 0.75814223
0.87462081 0.97378988]
[1.27346865 0.73175293 1.35820425 1.08405559 0.97660218 1.31912378
0.62859619 0.94765808]]
当我尝试将其读入程序时
inputMatrix = np.loadtxt("testing789.txt", dtype = 'i' , delimiter=' ')
print(inputMatrix)
我的问题是文件中的[
和]
是无法转换为 int32 的字符串。 有没有一种有效的方法来读取这个矩阵?
而不是像这样将矩阵写入文件: myFile.write(str(matrix))
,
像这样写它自动格式化: np.savetxt(fileName.txt, matrix)
最后一件事:从 txt 文件中加载矩阵,如下所示:
inputMatrix = np.loadtxt("testing789.txt", dtype = 'f', delimiter=' ')
其中使用dtype = 'f'
而不是i
以便矩阵值不四舍五入。
这里的正确答案是,如果可以的话,将源文件修复为标准格式,例如np.savetxt
所写的格式。 然后,您可以通过np.loadtxt
简单地阅读它。 如果那不可能,请继续阅读。
由于您的行似乎分为多行,因此我最初的建议(只需删除[
和]
,然后进行解析)将行不通。 相反,您可以在正确的位置添加逗号,以便该字符串是有效的 python 列表列表,并使用ast.literal_eval
对其进行评估。 然后,从这个列表列表中构建一个 numpy 数组。
import ast
import re
with open("fname.txt") as f:
fc = re.sub(" +", ", ", f.read())
lst = ast.literal_eval(fc)
arr = np.array(lst)
正则表达式" +"
只匹配一个或多个空格,用逗号代替。
现在arr
是预期的数组:
array([[0.26263508, 0.89992943, 0.62171512, 0.20750958, 0.21195397,
0.97217826, 0.61573457, 0.05643889],
[0.33188798, 0.32016444, 0.92051048, 0.75572024, 0.20247452,
0.37400282, 0.10935296, 0.63343081],
[0.87017165, 0.7283508 , 0.80314653, 0.80094718, 0.74024014,
0.16330332, 0.76579785, 0.75177055],
[0.2629302 , 0.59727507, 0.60866212, 0.29746334, 0.54587234,
0.43876005, 0.75007362, 0.89742691],
[0.05300406, 0.83342629, 0.19291691, 0.83372532, 0.98122163,
0.7815009 , 0.59635085, 0.9700382 ],
[0.69259902, 0.42779514, 0.04766533, 0.62205107, 0.71423376,
0.85045446, 0.31985818, 0.15338853],
[0.26947509, 0.41946874, 0.87206754, 0.35849082, 0.94756447,
0.59001803, 0.41028535, 0.85643487],
[0.87299386, 0.70986812, 0.87212445, 0.30309828, 0.31214338,
0.33387522, 0.52875374, 0.75712628],
[0.51605143, 0.64374971, 0.37821579, 0.77055732, 0.12504581,
0.75814223, 0.87462081, 0.97378988],
[1.27346865, 0.73175293, 1.35820425, 1.08405559, 0.97660218,
1.31912378, 0.62859619, 0.94765808]])
如果您没有将行分成多行(即如果您的文件看起来像这样:)
[[0.26263508 0.89992943 0.62171512 0.20750958 0.21195397 0.97217826 0.61573457 0.05643889]
[0.33188798 0.32016444 0.92051048 0.75572024 0.20247452 0.37400282 0.10935296 0.63343081]
[0.87017165 0.7283508 0.80314653 0.80094718 0.74024014 0.16330332 0.76579785 0.75177055]
[0.2629302 0.59727507 0.60866212 0.29746334 0.54587234 0.43876005 0.75007362 0.89742691]
[0.05300406 0.83342629 0.19291691 0.83372532 0.98122163 0.7815009 0.59635085 0.9700382 ]
[0.69259902 0.42779514 0.04766533 0.62205107 0.71423376 0.85045446 0.31985818 0.15338853]
[0.26947509 0.41946874 0.87206754 0.35849082 0.94756447 0.59001803 0.41028535 0.85643487]
[0.87299386 0.70986812 0.87212445 0.30309828 0.31214338 0.33387522 0.52875374 0.75712628]
[0.51605143 0.64374971 0.37821579 0.77055732 0.12504581 0.75814223 0.87462081 0.97378988]
[1.27346865 0.73175293 1.35820425 1.08405559 0.97660218 1.31912378 0.62859619 0.94765808]]
您可以只读入文件,删除[
和]
并去除前导空格,然后使用np.genfromtxt
读取修改后的字符串:
import io
fc = io.StringIO()
with open("fname.txt") as f:
for line in f:
fc.write(line.replace("[", "").replace("]", "").strip())
fc.write("\n")
fc.seek(0)
arr = np.genfromtxt(fc, delimiter=" ")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.