简体   繁体   中英

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 . My file contains a huge number of 0 and 1 separated by whitespaces. 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']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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