Python 通过合并两个连续列读取numpy数组中的文本文件
我正在尝试使用numpy.loadtxt将10Go文本文件读入numpy数组。我的文件包含大量由空格分隔的0和1。我想将两个值列合并到一列中,如下例所述。在这种情况下,我需要使用哪个分隔符 提前感谢您的重播 例如: 由此:Python 通过合并两个连续列读取numpy数组中的文本文件,python,numpy,Python,Numpy,我正在尝试使用numpy.loadtxt将10Go文本文件读入numpy数组。我的文件包含大量由空格分隔的0和1。我想将两个值列合并到一列中,如下例所述。在这种情况下,我需要使用哪个分隔符 提前感谢您的重播 例如: 由此: 0 1 1 1 1 0 0 0 1 0 1 0 1 1 1 0 0 0 0 0 0 1 1 1 到 当你写01时,它等于1吗?或者01必须保持为字符串?最简单的方法可能是将文件加载到数组a中,然后10*a[:,0::2]+a[:,1::2]。 array [ [01,1
0 1 1 1 1 0 0 0
1 0 1 0 1 1 1 0
0 0 0 0 0 1 1 1
到
当你写01时,它等于1吗?或者01必须保持为字符串?最简单的方法可能是将文件加载到数组a中,然后10*a[:,0::2]+a[:,1::2]。
array [ [01,11,10,00],
[10,10,11,10],
[00,00,01,11]
]
import numpy as np
txtcontent = np.loadtxt('txtfile.txt')
txtcontent = txtcontent.astype(int)
txtcontent = txtcontent.astype('str')
x1 = [''.join(txtcontent[i]) for i in range(len(txtcontent))]
output = [[x[i:i+2] for i in range(0,len(x),2)] for x in x1]
print(output)
[['01', '11', '10', '00'], ['10', '10', '11', '10'], ['00', '00', '01', '11']]