简体   繁体   English

通过合并两个连续列来读取 numpy 数组中的文本文件

[英]Read text file in numpy array by merging two consecutive columns

I'm trying to read a text file of 10Go into a numpy array using numpy.loadtxt .我正在尝试使用numpy.loadtxt将 10Go 的文本文件读入 numpy 数组。 My file contains a huge number of 0 and 1 separated by whitespaces.我的文件包含大量由空格分隔的 0 和 1。 I want to merge two values (columns) in one column as described below in the example.我想在一列中合并两个值(列),如下面的示例中所述。 Which delimiter I need to use in this case?在这种情况下我需要使用哪个分隔符?

Thank you in advance for your replay.提前感谢您的重播。

Example:例子:

From this:由此:

0 1 1 1 1 0 0 0

1 0 1 0 1 1 1 0

0 0 0 0 0 1 1 1

To

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']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM