-2

I have a JSON file which is like this

{'work':'12234','product':'mobile','rating':'3'}
{'work':'12444','product':'mobile','rating':'4'}              
{'work':'12634','product':'tv','rating':'5'}
{'work':'12277','product':'ac','rating':'3'}

and so on

I want to convert it into a dataframe can anyone help me with this

James McGuigan
  • 7,542
  • 4
  • 26
  • 29
  • 3
    Does this answer your question? [Convert list of dictionaries to a pandas DataFrame](https://stackoverflow.com/questions/20638006/convert-list-of-dictionaries-to-a-pandas-dataframe) – sushanth Jul 11 '20 at 10:04
  • No it did not, that is a list of dictionaries, my file is as shown above – venkatesh_akhouri Jul 11 '20 at 10:21
  • Does this answer your question? [Python - How to convert JSON File to Dataframe](https://stackoverflow.com/questions/41168558/python-how-to-convert-json-file-to-dataframe) – MrNobody33 Jul 11 '20 at 10:23
  • 1
    Your file is not JSON. JSON expects property names to be enclosed in double quotes. – Pramote Kuacharoen Jul 11 '20 at 10:27

1 Answers1

0

Your file is not JSON. JSON expects property names to be enclosed in double quotes. But, you can work around it.

import pandas as pd 

with open('data.txt') as f:
    data = [eval(line) for line in f]
df = pd.DataFrame(data)
Pramote Kuacharoen
  • 1,496
  • 1
  • 5
  • 6