Python Forum
How to form a dataframe reading separate dictionaries from .txt file? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to form a dataframe reading separate dictionaries from .txt file? (/thread-1726.html)



How to form a dataframe reading separate dictionaries from .txt file? - Doug - Jan-23-2017

Hey! Got an issue here. I need to read each line of .txt file as separate dictionary. Then to organize data in columns by the key of each dictionary.
my .txt file looks as follows:
{'Name': 'John', 'Age': 27, 'Score': 23}
{'Name': 'Peter', 'Age': 29, 'Score': 25}
I need to print it out as:
Name   Age   Score
John    27     23
Peter    29     25

so far, got to this peace of code:
with open('test.txt') as f:
    data = f.readlines()
    data = [x.strip() for x in data]
import pandas as pd
data = pd.DataFrame(data)
print(data)

which would only print:
                                           0
0   {'Name': 'John', 'Age': 27, 'Score': 23}
1  {'Name': 'Peter', 'Age': 29, 'Score': 25}

Thank you and have a great day!


RE: How to form a dataframe reading separate dictionaries from .txt file? - PsyPy - Nov-09-2020

Hey there! This is an old thread but when you read the file you get a list of strings. That is, you don't get a list of dictionaries. Therefore, Pandas will not work as you need. If you use literal_eval() from the ast module when reading your file. Here is one way you can get the results you need:

from ast import literal_eval
import pandas as pd

with open('test.txt') as f:
    data = f.readlines()
    data = [literal_eval(x.strip()) for x in data]


data = pd.DataFrame(data)
data.head()
# Out[26]:
#    Name  Age  Score
# 0   John   27     23
# 1  Peter   29     25
Anyway, you've probably already solved this but if anyone else happens to find their way to this post when having the same problem.