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


Messages In This Thread
How to form a dataframe reading separate dictionaries from .txt file? - by Doug - Jan-23-2017, 01:32 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 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,419 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,424 Feb-17-2019, 06:26 AM
Last Post: scidam
  Write specific rows from pandas dataframe to csv file pradeepkumarbe 3 5,433 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