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?
#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


Messages In This Thread
RE: How to form a dataframe reading separate dictionaries from .txt file? - by PsyPy - Nov-09-2020, 09:24 AM

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 2,934 Nov-07-2023, 10:56 PM
Last Post: zlim
  Reading large crapy text file in anaconda to profile data syamatunuguntla 0 1,367 Nov-18-2022, 06:15 PM
Last Post: syamatunuguntla
  export dataframe to file.txt dramauh 5 3,201 Apr-21-2022, 01:23 AM
Last Post: sarahroxon7
  Export dataframe to xlsx - Error "zipfile.BadZipFile: File is not a zip file" Baggio 10 77,581 Mar-12-2021, 01:02 PM
Last Post: buran
  Convert Excel to .txt - Need each excel row to be separate .txt file cowboykevin05 2 5,854 Jan-03-2020, 06:29 PM
Last Post: Larz60+
  reading tab file Mandiph 1 2,755 Sep-05-2019, 01:03 PM
Last Post: ThomasL
  How to add a dataframe to an existing excel file wendysling 2 31,124 May-09-2019, 07:00 PM
Last Post: wendysling
  Reading a .dat file in Python mohd_umair 4 25,696 Apr-24-2019, 12:07 PM
Last Post: mohd_umair
  convert images into pixel dataframe into csv file using python synthex 3 18,826 Feb-17-2019, 06:26 AM
Last Post: scidam
  Write specific rows from pandas dataframe to csv file pradeepkumarbe 3 7,106 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