Search code examples
pythonpandasdataframepython-3.6startswith

How to skip rows from a file until a specific string without using any input code?


I have a txt file name 'test.txt' and want to skip the first rows until I get to the line with the string "~A".

At this line, I want to remove "~A" and create a dataframe with 3 columns named 'Stats1', 'Stats2' and 'Stats3' and the related values below.

Also, I do not want to use any 'input' function to skip rows while opening the file as I would like to generalize this to other files.

Here is how the file looks like:

Text 1

Text 2

Text 3

Text 4

~A Stats1 Stats2 Stats3

1 2 3

6 6 7

8 9 3

Thank you!


Solution

  • That should work:

    with open('test.txt') as f:
        for l in f:
            if l.startswith('~A'):
                columns = l.split()[1:]
                break
        df = pd.read_csv(f, names=columns, sep=' ')
    

    By looping through file f you consume unneeded lines.

    Output:

    >>> df
       Stats1  Stats2  Stats3
    0       1       2       3
    1       6       6       7
    2       8       9       3