Reputation:
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'),)
Upvotes: 1
Views: 41
Reputation: 5741
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),)
Upvotes: 2