从文件中读取每一行作为 python 中的 numpy 数组
Read each line from file as numpy arrays in python
我有一个包含 4 行的文件 'test.txt',如下所示:
[35,51,3701],[10,6,7700.75],[42,31,4700.5],[31,41,1700.25],[6,25,3901.25],[10,12,3301.5]
[4,13,3709.25],[13,24,3761.5]
[11,18,3101.25],[26,7,3671],[10,69,7800.75]
[5,5,3701.25]
我以第一行手动为例。这是我想做的:
arr = np.array([[35,51,3701],[10,6,7700.75],[42,31,4700.5],[31,41,1700.25],[6,25,3901.25],[10,12,3301.5]])
p, b, a = [], [], []
for i in range(0, len(arr)):
b = b + [arr[i][0]]
a = a + [arr[i][1]]
p = p + [arr[i][2]]
print (p)
print (b)
print (a)
结果是(这正是我想要的):
[3701.0, 7700.75, 4700.5, 1700.25, 3901.25, 3301.5]
[35.0, 10.0, 42.0, 31.0, 6.0, 10.0]
[51.0, 6.0, 31.0, 41.0, 25.0, 12.0]
但是,当我尝试逐行读取文件时,它不起作用。这是我使用的代码:
f = open('test.txt')
for line in f:
print(line)
arr = np.array([line.rstrip()])
p, b, a = [], [], []
for i in range(0, len(arr)):
b = b + [arr[i][0]]
a = a + [arr[i][1]]
p = p + [arr[i][2]]
print (p)
print (b)
print (a)
print ("\n\n")
和结果(不是预期的):
[35,51,3701],[10,6,7700.75],[42,31,4700.5],[31,41,1700.25],[6,25,3901.25],[10,12,3301.5]
['5']
['[']
['3']
[4,13,3709.25],[13,24,3761.5]
[',']
['[']
['4']
[11,18,3101.25],[26,7,3671],[10,69,7800.75]
['1']
['[']
['1']
[5,5,3701.25]
[',']
['[']
['5']
你能告诉我如何得到手动示例中的结果吗?
谢谢
问题在行arr = np.array([line.rstrip()])
。 line.rstrip()
returns 字符串,不是数值。可以用ast.literal_eval
来解析,例如:
from ast import literal_eval
f = open("a.txt", "r")
for line in f:
print(line)
arr = np.array(literal_eval(line.rstrip() + ","))
p, b, a = [], [], []
for i in range(len(arr)):
b = b + [arr[i][0]]
a = a + [arr[i][1]]
p = p + [arr[i][2]]
print(p)
print(b)
print(a)
print("\n\n")
打印:
[35,51,3701],[10,6,7700.75],[42,31,4700.5],[31,41,1700.25],[6,25,3901.25],[10,12,3301.5]
[3701.0, 7700.75, 4700.5, 1700.25, 3901.25, 3301.5]
[35.0, 10.0, 42.0, 31.0, 6.0, 10.0]
[51.0, 6.0, 31.0, 41.0, 25.0, 12.0]
[4,13,3709.25],[13,24,3761.5]
[3709.25, 3761.5]
[4.0, 13.0]
[13.0, 24.0]
[11,18,3101.25],[26,7,3671],[10,69,7800.75]
[3101.25, 3671.0, 7800.75]
[11.0, 26.0, 10.0]
[18.0, 7.0, 69.0]
[5,5,3701.25]
[3701.25]
[5.0]
[5.0]
IIUC,你可以试试ast.literal_eval
:
from ast import literal_eval
for line in f: # use your file here
a = np.array(literal_eval(f'[{line}]'))
p,b,a = a.T.tolist()
print(p)
print(b)
print(a)
print('---')
或者,如果您不想使用 numpy
,请使用:
p,b,a = zip(*literal_eval(f'[{line}]'))
输出:
[35.0, 10.0, 42.0, 31.0, 6.0, 10.0]
[51.0, 6.0, 31.0, 41.0, 25.0, 12.0]
[3701.0, 7700.75, 4700.5, 1700.25, 3901.25, 3301.5]
---
[4.0, 13.0]
[13.0, 24.0]
[3709.25, 3761.5]
---
[11.0, 26.0, 10.0]
[18.0, 7.0, 69.0]
[3101.25, 3671.0, 7800.75]
---
[5.0]
[5.0]
[3701.25]
---
我有一个包含 4 行的文件 'test.txt',如下所示:
[35,51,3701],[10,6,7700.75],[42,31,4700.5],[31,41,1700.25],[6,25,3901.25],[10,12,3301.5]
[4,13,3709.25],[13,24,3761.5]
[11,18,3101.25],[26,7,3671],[10,69,7800.75]
[5,5,3701.25]
我以第一行手动为例。这是我想做的:
arr = np.array([[35,51,3701],[10,6,7700.75],[42,31,4700.5],[31,41,1700.25],[6,25,3901.25],[10,12,3301.5]])
p, b, a = [], [], []
for i in range(0, len(arr)):
b = b + [arr[i][0]]
a = a + [arr[i][1]]
p = p + [arr[i][2]]
print (p)
print (b)
print (a)
结果是(这正是我想要的):
[3701.0, 7700.75, 4700.5, 1700.25, 3901.25, 3301.5]
[35.0, 10.0, 42.0, 31.0, 6.0, 10.0]
[51.0, 6.0, 31.0, 41.0, 25.0, 12.0]
但是,当我尝试逐行读取文件时,它不起作用。这是我使用的代码:
f = open('test.txt')
for line in f:
print(line)
arr = np.array([line.rstrip()])
p, b, a = [], [], []
for i in range(0, len(arr)):
b = b + [arr[i][0]]
a = a + [arr[i][1]]
p = p + [arr[i][2]]
print (p)
print (b)
print (a)
print ("\n\n")
和结果(不是预期的):
[35,51,3701],[10,6,7700.75],[42,31,4700.5],[31,41,1700.25],[6,25,3901.25],[10,12,3301.5]
['5']
['[']
['3']
[4,13,3709.25],[13,24,3761.5]
[',']
['[']
['4']
[11,18,3101.25],[26,7,3671],[10,69,7800.75]
['1']
['[']
['1']
[5,5,3701.25]
[',']
['[']
['5']
你能告诉我如何得到手动示例中的结果吗? 谢谢
问题在行arr = np.array([line.rstrip()])
。 line.rstrip()
returns 字符串,不是数值。可以用ast.literal_eval
来解析,例如:
from ast import literal_eval
f = open("a.txt", "r")
for line in f:
print(line)
arr = np.array(literal_eval(line.rstrip() + ","))
p, b, a = [], [], []
for i in range(len(arr)):
b = b + [arr[i][0]]
a = a + [arr[i][1]]
p = p + [arr[i][2]]
print(p)
print(b)
print(a)
print("\n\n")
打印:
[35,51,3701],[10,6,7700.75],[42,31,4700.5],[31,41,1700.25],[6,25,3901.25],[10,12,3301.5]
[3701.0, 7700.75, 4700.5, 1700.25, 3901.25, 3301.5]
[35.0, 10.0, 42.0, 31.0, 6.0, 10.0]
[51.0, 6.0, 31.0, 41.0, 25.0, 12.0]
[4,13,3709.25],[13,24,3761.5]
[3709.25, 3761.5]
[4.0, 13.0]
[13.0, 24.0]
[11,18,3101.25],[26,7,3671],[10,69,7800.75]
[3101.25, 3671.0, 7800.75]
[11.0, 26.0, 10.0]
[18.0, 7.0, 69.0]
[5,5,3701.25]
[3701.25]
[5.0]
[5.0]
IIUC,你可以试试ast.literal_eval
:
from ast import literal_eval
for line in f: # use your file here
a = np.array(literal_eval(f'[{line}]'))
p,b,a = a.T.tolist()
print(p)
print(b)
print(a)
print('---')
或者,如果您不想使用 numpy
,请使用:
p,b,a = zip(*literal_eval(f'[{line}]'))
输出:
[35.0, 10.0, 42.0, 31.0, 6.0, 10.0]
[51.0, 6.0, 31.0, 41.0, 25.0, 12.0]
[3701.0, 7700.75, 4700.5, 1700.25, 3901.25, 3301.5]
---
[4.0, 13.0]
[13.0, 24.0]
[3709.25, 3761.5]
---
[11.0, 26.0, 10.0]
[18.0, 7.0, 69.0]
[3101.25, 3671.0, 7800.75]
---
[5.0]
[5.0]
[3701.25]
---