I have text file like this pattern (2 first rows have 5 columns, then third has 3, then again 2rows 5columns, 1 rows 3 colums)
1, 2, 3, 4, 5
6, 7, 8, 9, 10
11, 12, 13
14, 15, 16, 17, 18
19, 20, 21, 22, 23
24, 25, 26
I am a very newbie to python and numpy, I would like to have output file like this
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26
It mean that I need 2 rows and 13 colums text file from the input file.
CodePudding user response:
Your data is irregular, so reading it is quite tough: Here is one approach
import numpy as np
dat = ','.join(np.loadtxt('file.txt',str, delimiter='\n')).split(',')
np.array(dat, float).reshape(2, -1)
array([[ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.],
[14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26.]])