Python Forum
How to form a dataframe reading separate dictionaries from .txt file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to form a dataframe reading separate dictionaries from .txt file?
#1
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!
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to most effectively unpack list of name-value pair dictionaries in a dataframe? zlim 1 615 Nov-07-2023, 10:56 PM
Last Post: zlim
  Reading large crapy text file in anaconda to profile data syamatunuguntla 0 811 Nov-18-2022, 06:15 PM
Last Post: syamatunuguntla
  export dataframe to file.txt dramauh 5 1,884 Apr-21-2022, 01:23 AM
Last Post: sarahroxon7
  Export dataframe to xlsx - Error "zipfile.BadZipFile: File is not a zip file" Baggio 10 61,418 Mar-12-2021, 01:02 PM
Last Post: buran
  Convert Excel to .txt - Need each excel row to be separate .txt file cowboykevin05 2 4,735 Jan-03-2020, 06:29 PM
Last Post: Larz60+
  reading tab file Mandiph 1 2,152 Sep-05-2019, 01:03 PM
Last Post: ThomasL
  How to add a dataframe to an existing excel file wendysling 2 28,054 May-09-2019, 07:00 PM
Last Post: wendysling
  Reading a .dat file in Python mohd_umair 4 23,480 Apr-24-2019, 12:07 PM
Last Post: mohd_umair
  convert images into pixel dataframe into csv file using python synthex 3 17,423 Feb-17-2019, 06:26 AM
Last Post: scidam
  Write specific rows from pandas dataframe to csv file pradeepkumarbe 3 5,432 Oct-18-2018, 09:33 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020