Reading a large amount of numbers in python -


i trying read large amount of numbers (8112 in total) , rearrange them 6 columns. first, want add 52 numbers first column, want add 52 second, 52 third, , on. when have resultant 6 columns, each containing 52 numbers, want continue reading in same way till end of data. have try this:

with open('file.dat') f:     line = f.read().split()         row in range(len(line)):         col in range(6):             print line[row + 52*col],            print   

the code not reading correctly numbers , not got till end. stooping after reading 7000 numbers. index error: list index out of range.

the input file contains numbers listed this:

-0.001491728991 -0.001392067804 -0.001383514062 -0.000777354202 -0.000176516325 -0.00066003232 0.001491728657 0.001392067465 0.00138351373 0.00077735388 0.000176516029 0.000660032023 -0.001491728966 -0.001392067669 -0.001383513988 -0.000777354111 -0.000176516303 2.5350931e-05 -0.000660032277 0.001491728631 0.00139206733 0.001383513657 0.000777353789 0.000176516006 0.000660031981 -0.003692742099 -0.003274685372 -0.001504168916 0.003692740966 0.003274684254 0.001504167874 -0.003692741847 -0.003274685132 -0.001504168791 (...)

(8112 numbers in total)

try this:

data = range(8112) # replace input file  col_size = 52 col_count = 6 batch_size = (col_size*col_count) # split input batches of 6 columns of 52 entries each batch in range(0,len(data),batch_size):     # rearrange line data 6 column format     cols = zip(*[data[x:x+col_size] x in range(batch,batch+batch_size,col_size)])     c in cols:         print c 

output:

(0, 52, 104, 156, 208, 260) (1, 53, 105, 157, 209, 261) ... (50, 102, 154, 206, 258, 310) (51, 103, 155, 207, 259, 311) (312, 364, 416, 468, 520, 572) (313, 365, 417, 469, 521, 573) ... (362, 414, 466, 518, 570, 622) (363, 415, 467, 519, 571, 623) (624, 676, 728, 780, 832, 884) (625, 677, 729, 781, 833, 885) ... 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -