MotaF MotaF - 8 days ago 6
Python Question

How to read file formatted in uneven manner?

I have file formatted like this

9 0.0351562 0.0713449 -11.4664 -25.9366 -27.955 4 2.38188
10 0.0390625 0.0507383 -12.9466 -30.2437 -29.1045 4 0.616179
11 0.0429688 0.0629145 -12.0125 -37.1503 -32.2795 4 0.569045
12 0.046875 0.046937 -13.2848 -31.9606 -32.5367 4 0.128286
13 0.0507812 0.0541174 -12.6666 -30.7398 -27.4705 4 1.38345


Regarding the Python code my idea was to use regex.

for line in s.splitlines():
lst = [i.strip() for j in re.findall(regex, line) for i in j if j]
print(lst)


But how should the regex look like?


The C code line that wrote the file:

fprintf(inf, "%d %g %g %g %g %g %g %g\n", i,frq1,
spec[i],dbspec, naive_spec[i], dtemp[i],
dof[i], Fvalues[i]);


I have 8 variables in line, 1 integer and 7 doubles in Python syntax.

Answer
with open('path/to/file') as infile:
    for line in infie:
        nums = [float(i) for i in line.split()]
        # do stuff with nums