Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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 如何将每行列数可变的txt文件中的数据加载到numpy数组中?_Python_Arrays_Numpy_File Handling - Fatal编程技术网

Python 如何将每行列数可变的txt文件中的数据加载到numpy数组中?

Python 如何将每行列数可变的txt文件中的数据加载到numpy数组中?,python,arrays,numpy,file-handling,Python,Arrays,Numpy,File Handling,我的数据文件如下所示: 我想将此数据加载到numpy数组中。我该怎么做 如果我使用loadtxt(文件名),它会给出错误: raise ValueError(errmsg) ValueError: Some errors were detected ! 如果我使用genfromtxt(filename,delimiter=“”),它会给出相同的错误,即使这应该可以修复它 如果我使用以下选项: from array import array N=84 # max number of colum

我的数据文件如下所示:

我想将此数据加载到numpy数组中。我该怎么做

如果我使用
loadtxt(文件名)
,它会给出错误:

raise ValueError(errmsg)
ValueError: Some errors were detected !
如果我使用
genfromtxt(filename,delimiter=“”)
,它会给出相同的错误,即使这应该可以修复它

如果我使用以下选项:

from array import array
N=84 # max number of columns in any row in the data file
with open('C:/Users/hp1/Desktop/ClusterAnalysis/hierarchical_result.txt',"r") as f:
        all_data=[x.split() for x in f.readlines()]
        a=array([map(int,x) for x in all_data[:N]])
我得到这个错误:

TypeError:array()参数1必须是unicode字符,而不是list

编辑:这是数据文件中的所有数据:

61 81
2 28
13 31
59 64
36 63
45 58
3 73
47 51
33 68
1 72
12 84
3 73 12 84
1 72 3 73 12 84
6 83
27 42
66 6 83
54 77
60 54 77
39 40
10 19
49 79
22 76
61 81 60 54 77
65 61 81 60 54 77
8 65 61 81 60 54 77
66 6 83 8 65 61 81 60 54 77
71 47 51
18 25
59 64 18 25
32 59 64 18 25
11 34
20 26
27 42 20 26
69 27 42 20 26
16 62
43 16 62
30 45 58
85 30 45 58
56 85 30 45 58
17 11 34
22 76 32 59 64 18 25
29 39 40
14 57
44 14 57
7 24
78 2 28
15 37
70 15 37
48 70 15 37
80 29 39 40
4 9
75 43 16 62
13 31 75 43 16 62
74 13 31 75 43 16 62
36 63 17 11 34
53 36 63 17 11 34
46 1 72 3 73 12 84
23 52
38 66 6 83 8 65 61 81 60 54 77
82 38 66 6 83 8 65 61 81 60 54 77
10 19 56 85 30 45 58
33 68 10 19 56 85 30 45 58
5 49 79
78 2 28 4 9
55 80 29 39 40
67 55 80 29 39 40
7 24 67 55 80 29 39 40
35 48 70 15 37
69 27 42 20 26 35 48 70 15 37
41 82 38 66 6 83 8 65 61 81 60 54 77
50 69 27 42 20 26 35 48 70 15 37
33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
7 24 67 55 80 29 39 40 50 69 27 42 20 26 35 48 70 15 37
46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
22 76 32 59 64 18 25 46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
7 24 67 55 80 29 39 40 50 69 27 42 20 26 35 48 70 15 37 22 76 32 59 64 18 25 46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
53 36 63 17 11 34 7 24 67 55 80 29 39 40 50 69 27 42 20 26 35 48 70 15 37 22 76 32 59 64 18 25 46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
78 2 28 4 9 53 36 63 17 11 34 7 24 67 55 80 29 39 40 50 69 27 42 20 26 35 48 70 15 37 22 76 32 59 64 18 25 46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
74 13 31 75 43 16 62 78 2 28 4 9 53 36 63 17 11 34 7 24 67 55 80 29 39 40 50 69 27 42 20 26 35 48 70 15 37 22 76 32 59 64 18 25 46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
44 14 57 74 13 31 75 43 16 62 78 2 28 4 9 53 36 63 17 11 34 7 24 67 55 80 29 39 40 50 69 27 42 20 26 35 48 70 15 37 22 76 32 59 64 18 25 46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
5 49 79 44 14 57 74 13 31 75 43 16 62 78 2 28 4 9 53 36 63 17 11 34 7 24 67 55 80 29 39 40 50 69 27 42 20 26 35 48 70 15 37 22 76 32 59 64 18 25 46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
71 47 51 5 49 79 44 14 57 74 13 31 75 43 16 62 78 2 28 4 9 53 36 63 17 11 34 7 24 67 55 80 29 39 40 50 69 27 42 20 26 35 48 70 15 37 22 76 32 59 64 18 25 46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
23 52 71 47 51 5 49 79 44 14 57 74 13 31 75 43 16 62 78 2 28 4 9 53 36 63 17 11 34 7 24 67 55 80 29 39 40 50 69 27 42 20 26 35 48 70 15 37 22 76 32 59 64 18 25 46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77
21 23 52 71 47 51 5 49 79 44 14 57 74 13 31 75 43 16 62 78 2 28 4 9 53 36 63 17 11 34 7 24 67 55 80 29 39 40 50 69 27 42 20 26 35 48 70 15 37 22 76 32 59 64 18 25 46 1 72 3 73 12 84 33 68 10 19 56 85 30 45 58 41 82 38 66 6 83 8 65 61 81 60 54 77

numpy.genfromtxt
不处理可变长度行。您应该自己解析
txt

python3.x

import numpy as np
N = 84 # max number of columns in any row in the data file
with open('C:/Users/hp1/Desktop/ClusterAnalysis/hierarchical_result.txt',"r") as f:
        all_data = [x.split() for x in f.readlines()]
        output = np.array([list(map(int,x))[:N] for x in all_data])

numpy.genfromtxt
不处理可变长度行。您应该自己解析
txt

python3.x

import numpy as np
N = 84 # max number of columns in any row in the data file
with open('C:/Users/hp1/Desktop/ClusterAnalysis/hierarchical_result.txt',"r") as f:
        all_data = [x.split() for x in f.readlines()]
        output = np.array([list(map(int,x))[:N] for x in all_data])
nums
是一个数字列表。无法将其转换为二维数字数组

但是通过简单的
读取
我得到了一个带有换行符的字符串:

In [311]: with open('stack44755004.txt') as f:
     ...:     alldata = f.read()

In [312]: alldata
Out[312]: '61 81\n2 28\n13 31\n3 73 12 84\n6 83\n'
split
将其视为空格,因此我得到一个字符串列表:

In [313]: alldata.split()
Out[313]: ['61', '81', '2', '28', '13', '31', '3', '73', '12', '84', '6', '83']
np.array
可以将其转换为整数数组

In [314]: np.array(alldata.split(),int)
Out[314]: array([61, 81,  2, 28, 13, 31,  3, 73, 12, 84,  6, 83])
此方法会释放所有行信息。这重要吗

有多种方法可以将
nums
转换为数组。例如,它可以写入一个零填充数组。但是如果你不知道你想要什么,我不确定这是否值得


有各种各样的
padding
问题。我脑海中浮现的一个工具是
itertools.zip\u longest
(Python3版本):

Out[317]:
在[318]中:列表(itertools.zip_longest(*nums,fillvalue=0))
[318]:[(61,2,13,3,6),(81,28,31,73,83),(0,0,0,12,0),(0,0,0,84,0)]
[319]中的np.数组
出[319]:
数组([[61,2,13,3,6],
[81, 28, 31, 73, 83],
[ 0,  0,  0, 12,  0],
[ 0,  0,  0, 84,  0]])
在[320]中:
出[320]:
数组([[61,81,0,0],
[ 2, 28,  0,  0],
[13, 31,  0,  0],
[ 3, 73, 12, 84],
[ 6, 83,  0,  0]])
nums
是一个数字列表。无法将其转换为二维数字数组

但是通过简单的
读取
我得到了一个带有换行符的字符串:

In [311]: with open('stack44755004.txt') as f:
     ...:     alldata = f.read()

In [312]: alldata
Out[312]: '61 81\n2 28\n13 31\n3 73 12 84\n6 83\n'
split
将其视为空格,因此我得到一个字符串列表:

In [313]: alldata.split()
Out[313]: ['61', '81', '2', '28', '13', '31', '3', '73', '12', '84', '6', '83']
np.array
可以将其转换为整数数组

In [314]: np.array(alldata.split(),int)
Out[314]: array([61, 81,  2, 28, 13, 31,  3, 73, 12, 84,  6, 83])
此方法会释放所有行信息。这重要吗

有多种方法可以将
nums
转换为数组。例如,它可以写入一个零填充数组。但是如果你不知道你想要什么,我不确定这是否值得


有各种各样的
padding
问题。我脑海中浮现的一个工具是
itertools.zip\u longest
(Python3版本):

Out[317]:
在[318]中:列表(itertools.zip_longest(*nums,fillvalue=0))
[318]:[(61,2,13,3,6),(81,28,31,73,83),(0,0,0,12,0),(0,0,0,84,0)]
[319]中的np.数组
出[319]:
数组([[61,2,13,3,6],
[81, 28, 31, 73, 83],
[ 0,  0,  0, 12,  0],
[ 0,  0,  0, 84,  0]])
在[320]中:
出[320]:
数组([[61,81,0,0],
[ 2, 28,  0,  0],
[13, 31,  0,  0],
[ 3, 73, 12, 84],
[ 6, 83,  0,  0]])

我已经使用pandas解决了这个问题,您可以在这里指定所需的列。如果列数较少,则将其设置为NaN。
您必须知道最大列数,但使用readlines、split和列表理解很容易检测到这一点。

我使用了pandas来解决这个问题,您可以在其中指定所需的列。如果列数较少,则将其设置为NaN。
您必须知道最大列数,但使用readlines、split和列表理解很容易检测到这一点。

如果您想用最大列数填充每一行,您必须自己实现它。大意是:

import numpy as np

def pad_list(lst, padding, default=0):
    return lst + (padding - len(lst))*[default]

N = 84 # max number of columns in any row in the data file
with open('/path/to/file',"r") as f:
        all_data=(map(int, x.split()) for x in f)
        a = np.array([pad_list(list(x), N) for x in all_data])

但是,为了给您一个数值数组而不是对象类型数组,您需要知道实际的最大列数。因此,在计算时要小心。

如果您想用最大列数填充每一行,您必须自己实现它。大意是:

import numpy as np

def pad_list(lst, padding, default=0):
    return lst + (padding - len(lst))*[default]

N = 84 # max number of columns in any row in the data file
with open('/path/to/file',"r") as f:
        all_data=(map(int, x.split()) for x in f)
        a = np.array([pad_list(list(x), N) for x in all_data])

但是,为了给您一个数值数组而不是对象类型数组,您需要知道实际的最大列数。所以要小心弄清楚这一点。

你到底期望什么?阵列的形状应该是什么
Numpy
具有真正的多维数组,不支持交错数组。编辑:如果你使用
dtype=object
,它们确实支持锯齿数组,但是你基本上失去了“numpy1”的大部分功能查看
all_data
,确保所有字符串都可以转换为
int
,然后尝试
x=[int(i)for i in all_data]
。这些都是数字吗?然后
np.array(x)
应该可以工作,生成一个一维整数数组。@juanpa.arrivillaga我不知道,编程不完全是我的事。我只是不得不将其用于这项特殊工作。@Kristada673您需要指定您期望的输出,否则我们将如何帮助您?@hpaulj我尝试了
np.array(x)
,没有成功。您到底期望什么?阵列的形状应该是什么
Numpy
具有真正的多维数组,不支持交错数组。编辑:如果你使用
dtype=object
,它们确实支持锯齿数组,但是你基本上失去了“numpy1”的大部分功能查看
all_data
,确保所有字符串都可以转换为
int
,然后尝试
x=[int(i)for i in all_data]
。这些都是数字吗?然后
np.array(x)
应该