1

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!

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

That is it, @Yevhen! That worked perfectly for me. Thanks a lot
@LucasA please consider accepting the answer by checking the tick mark to the left of the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.