Home > Back-end >  in python how do i convert a list of strings to pandas data frame
in python how do i convert a list of strings to pandas data frame

Time:11-11

i dont often reach out to forums but i am getting really confused with this and hope someone could point me in the right direction. i would also like to point out i am new to python so still getting used to lists and dicts etc. ok so my problem, i am reading a text file which looks like this which as newline after each one.

{'Key1': 'something', 'Key2': 1636535714738, 'Key3': 'Foo', 'Key4': '0.18916000'}
{'Key1': 'something', 'Key2': 1636535729352, 'Key3': 'Foo', 'Key4': '0.18915000'}
{'Key1': 'something', 'Key2': 1636535736276, 'Key3': 'Foo', 'Key4': '0.18922000'}
{'Key1': 'something', 'Key2': 1636535749584, 'Key3': 'Foo', 'Key4': '0.18934000'}
{'Key1': 'something', 'Key2': 1636535777668, 'Key3': 'Foo', 'Key4': '0.18958000'}

and i am trying to convert it to pandas data frame, i think i need to convert it to a dict of dicts first or maybe a list of dicts. because the original is a string i am having issues with converting it. i think i have 2 problems but if i can solve the first (convert it to the correct format) then the second will be fixed (converting to a data frame), all i have so far is below but this gives me an error.

import pandas as pd
 
with open("c:/file.log") as f:

    for line in f:
        res = dict(eval(line))
        new = pd.DataFrame.from_dict(res)
f.close

print(type(new))
print(new)

CodePudding user response:

Use ast:

import pandas as pd
import ast

file1 = open('c:/file.log', 'r')
Lines = file1.readlines()
 
listDict = []

for line in Lines:
    listDict.append(ast.literal_eval(line.strip()))

df = pd.DataFrame.from_dict(listDict)
print(df)
  • Related