2024-04-25 05:23:26 发布
网友
我有一个txt文件,看起来像
col1 col2 col3 1 4 7 2 5 8 3 6 9
我正在尝试生成一个包含列数据的列表:
但是下面的代码生成一个行数据列表,即[ [1,4,7], ... ]。在
[ [1,4,7], ... ]
blah = np.genfromtxt(scrubbed_file, skip_header=1) for bluh in blah: print(bluh)
有没有一个简单的方法来实现这一点?在
python有一个非常方便的特性,即zip(*list):
zip(*list)
blah = np.genfromtxt(scrubbed_file, skip_header=1) for bluh in zip(*blah): print(bluh)
您可以像这样转置结果数组: blah = np.genfromtxt(scrubbed_file, skip_header=1).T 这将有效地将数组放在它的一边,从而得到您想要的数据格式。在
blah = np.genfromtxt(scrubbed_file, skip_header=1).T
使用genfromtext中的unpack变量:
genfromtext
np.genfromtxt('text.csv', delimiter=',', unpack=True, skip_header=1)
它给出了以下内容:
python有一个非常方便的特性,即
zip(*list)
:您可以像这样转置结果数组:
blah = np.genfromtxt(scrubbed_file, skip_header=1).T
这将有效地将数组放在它的一边,从而得到您想要的数据格式。在使用
genfromtext
中的unpack变量:它给出了以下内容:
^{pr2}$相关问题 更多 >
编程相关推荐