1

How do I convert a text file with values separated to a dataframe?

test.txt:

abc89321 , (('mike', 'A1 passed'),)
abc24862 , (('ross', 'm1 passed'),)
abc17695 , (('carter', 'c2 failed'),)

I have a text file with values like this, how to make these values into a dataframe with two columns?

Expected output:

a           b  
abc89321   (('mike', 'A1 passed'),)
abc24862   (('ross', 'm1 passed'),)
abc17695   (('carter', 'c2 failed'),)

1 Answer 1

2

Use .read_csv() in combination with ast.literal_eval:

from ast import literal_eval

df = pd.read_csv(
    'test.csv',
    sep=' , ',
    names=['a', 'b'],
    engine='python',
    converters={'b': literal_eval}
)

df

          a                       b
0  abc89321    ((mike, A1 passed),)
1  abc24862    ((ross, m1 passed),)
2  abc17695  ((carter, c2 failed),)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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