How to find the column number for a specific value and the CSV file at that location

Viewed 192

I'm trying to split a csv file at a specific row with a specific value, but i cannot figure out how to do it. The csv file is a data export from a program we use and it consist of two different parts. The file looks like this and the second part is always started with '[Faces]'.

[Name]
Plane 1750

[Data]
Node Number, X [ m ], Y [ m ], Z [ m ], AV WF
0, -1.96213058e+02, -2.73303375e+02, 1.75000000e+00, 2.01742917e-01
1, -1.96173523e+02, -2.73252655e+02, 1.75000000e+00, 2.02091664e-01
606479, -2.06638428e+02, 2.93843475e+02, 1.75000000e+00, 3.21377516e-01
606480, -2.05079956e+02, 2.94933014e+02, 1.75000000e+00, 3.27591240e-01

[Faces]
400, 335, 336, 339, 338
196775, 644, 610, 611, 196774
1658, 1657, 1656, 196787, 196788
1562, 1561, 1439, 1438, 196794

Now I would like to know how I could split the file at the location of the value '[Faces]' and save all the data below that in a new csv file.

This execution is part of a script which already uses pandas and numpy.

1 Answers

Try to consume lines until we meet [Faces] line:

with open('data.txt') as fp:
    while fp.readline().strip() != '[Faces]':
        pass
    df = pd.read_csv(fp, header=None, skipinitialspace=True)

with open('faces.csv') as fp:
    fp.write('[Faces]\n')
    df.to_csv(fp, index=False)

Content of faces.csv:

[Faces]
0,1,2,3,4
400,335,336,339,338
196775,644,610,611,196774
1658,1657,1656,196787,196788
1562,1561,1439,1438,196794

Update

Without Pandas:

with open('data.txt') as inp, open('faces.csv', 'w') as out:
    while inp.readline().strip() != '[Faces]':
        pass
    out.writelines(['Faces\n'] + inp.readlines())

Update 2

import shutil

with open('data.txt', 'r') as inp, \
     open('faces.csv', 'w') as out, \
     open('temp.txt', 'w') as tmp:
    fp = tmp
    for line in inp:
        if line == '[Faces]\n':
            fp = out
        fp.write(line)

shutil.move('temp.txt', 'data2.txt')
Related