python - Spliting a string list to floats - memory error -


i can't handle splitting list of strings list of floats. open text file (.vol, contains text) last line extremely long line of numbers.

params1 437288.59375000 102574.20312500 -83.30001831
params2 437871.93750000 104981.40625000 362.10000610
params3 0.00000000
params4 342 1416 262
params5 583.34375000 2407.19995117 445.40002441
params6 20.00000000
params7 1.70000005
params8 126879264
values:
0.25564435 0.462439 0.1365 0.1367 26.00000000 (etc., there millions of values)

since it's 10th line of txt file, load them list by:

with open('d:/py/las21_test.vol') f: txt = [] line in f:     txt.append(line) 

and try convert string floats by:

a = [] vx in txt[9]:    try:        a.append(float(vx))    except valueerror:        pass print (a[0:20]) print (txt[9][0:20]) 

this give me results:

[0.0, 2.0, 5.0, 5.0, 6.0, 4.0, 4.0, 3.0, 5.0, 0.0, 4.0, 6.0, 2.0, 4.0, 3.0, 9.0, 0.0, 1.0, 3.0, 6.0] 0.25564435 0.462439  

what have list of correctly split floats, like:

[0.25564435, 0.462439] 

i used except valueerror omit whitespaces - when using float(txt[9]) value error. second issue: can't use txt[9].split because 'memory error'.

how can convert list of floats properly?

if understand correctly, cannot make txt[9].split() , map this: map(float, txt[9].split()) because txt[9] large.

you can try generator:

def float_generator(txt):         x = ''         c in txt:             if c != ' ':                 x += c             else:                 out = float(x)                 x = ''                 yield(out)  in float_generator(txt[9]):         print (i) 

Comments