Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将文件的行转换为numpy数组_Python_Numpy - Fatal编程技术网

Python 将文件的行转换为numpy数组

Python 将文件的行转换为numpy数组,python,numpy,Python,Numpy,我有一个文件,其中的数据是这样的 [1,2,3,4,5] [4,2,3,4,5,6] [3,12,3,34,5] [3,4,5,6,7,8] [2,3,4,5,6,6] [2,3,4,5,5,4,3] [1,2,3,23,2,1] [[1,2,3,4,5],[4,2,3,4,5,6]] 我只是想把它转换成numpy数组,类似这样的东西 [1,2,3,4,5] [4,2,3,4,5,6] [3,12,3,34,5] [3,4,5,6,7,8] [2,3,4,5,6,6] [2,3,4,5,5,

我有一个文件,其中的数据是这样的

[1,2,3,4,5]
[4,2,3,4,5,6]
[3,12,3,34,5]
[3,4,5,6,7,8]
[2,3,4,5,6,6]
[2,3,4,5,5,4,3]
[1,2,3,23,2,1]
[[1,2,3,4,5],[4,2,3,4,5,6]]
我只是想把它转换成numpy数组,类似这样的东西

[1,2,3,4,5]
[4,2,3,4,5,6]
[3,12,3,34,5]
[3,4,5,6,7,8]
[2,3,4,5,6,6]
[2,3,4,5,5,4,3]
[1,2,3,23,2,1]
[[1,2,3,4,5],[4,2,3,4,5,6]]
我尝试了以下代码:

import ast
import numpy as np
a = np.array([])
with open("test.txt","r") as f:
        for line in f:
                line = ast.literal_eval(line)
                np.append(line,a)
它只提供了
[]

with open('file.txt') as f:
    data = []
    for line in f:
        words = line.replace('[', ''). replace(']', '').replace('\n', '').split(',')
        data.append([int(i) for i in words])
如果要使用numpy阵列:

with open('file.txt') as f:
    data = []
    for line in f:
        words = line.replace('[', ''). replace(']', '').replace('\n', '').split(',')
        data.append(np.array([int(i) for i in words]))
在numpy中使用方法

import ast
from numpy import array

p = "PATH_TO_INPUT_FILE"
data = []
with open(p,"r") as f:
    for i in f.readlines():
        data.append(ast.literal_eval(i.strip()))
data = array(data)
print(data)
print(type(data))
输出:

[[1, 2, 3, 4, 5] [4, 2, 3, 4, 5, 6] [3, 12, 3, 34, 5] [3, 4, 5, 6, 7, 8]
 [2, 3, 4, 5, 6, 6] [2, 3, 4, 5, 5, 4, 3] [1, 2, 3, 23, 2, 1]]
<type 'numpy.ndarray'>
[[1,2,3,4,5][4,2,3,4,5,6][3,12,3,34,5][3,4,5,6,7,8]
[2, 3, 4, 5, 6, 6] [2, 3, 4, 5, 5, 4, 3] [1, 2, 3, 23, 2, 1]]

我尝试了以下代码:如果你也要泄露接下来发生的事情,它可能会很有用。@PaulPanzer当然要编辑了。我认为您需要交换
append
的参数,更重要的是重新分配给a<代码>np.append不能就地工作。顺便说一句,这不是一个合适的数组,因为维度不是规则的。它给出:[数组([1,2,3,4,5]),数组([4,2,3,4,5,6]),数组([3,12,3,34,5]),数组([3,4,5,7,8]),数组([2,3,4,5,6]),数组([2,3,4,5,5,4,3]),数组([1,2,23,2,1])]第一个会给你一个列表(这是你在问题中想要的)。第二个将生成np数组列表。对第一个输出使用np.array(data)以获得2d nparray@ggupta你对numpy有什么期望?您的列表没有相同的维度,如何将它们转换为nxm数组?