0

I have a file .csv like that:

ID;Name;Sports
1;Mark;["Football", "Volleyball"]
2;Luuk;["Fencing"]
3;Carl;["Swimming", "Basketball"]

My goal is to print on a file .json the following:

[
{
"ID": 1,
"Name": "Mark",
"Sports": ["Football", "Volleyball"]
}
,
{
"ID": 2,
"Name": "Luuk",
"Sports": ["Fencing"]
}
,
{
"ID": 3,
"Name": "Carl",
"Sports": ["Swimming", "Basketball"]
}
]

My code to produce the list:

from ast import literal_eval
import pandas as pd

df = pd.read_csv("test.csv", sep=";", converters={'Sports': literal_eval})

columns = list(df.columns)

json = []
for i in range(len(df)):
    line = {}
    for j in range(len(columns)):
        line[columns[j]] = df.iloc[i, j]
    json.append(line)

How can I smartly print my list in the same format written above?

LJG
  • 601
  • 1
  • 7
  • 15

2 Answers2

2

Use DataFrame.to_json:

from ast import literal_eval

df = pd.read_csv("test.csv", sep=";", converters={'Sports': literal_eval})


json = df.to_json(orient='records')
print (json)
[{"ID":1,"Name":"Mark","Sports":["Football","Volleyball"]},
 {"ID":2,"Name":"Luuk","Sports":["Fencing"]},
 {"ID":3,"Name":"Carl","Sports":["Swimming","Basketball"]}]

EDIT: If need create json file use DataFrame.to_dict:

from ast import literal_eval

df = pd.read_csv("test.csv", sep=";", converters={'Sports': literal_eval})

d = df.to_dict(orient='records')
print (d)
[{"ID":1,"Name":"Mark","Sports":["Football","Volleyball"]},
 {"ID":2,"Name":"Luuk","Sports":["Fencing"]},
 {"ID":3,"Name":"Carl","Sports":["Swimming","Basketball"]}]

import json
with open('result.json', 'w') as fp:
    json.dump(d, fp)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • My question however was about how to print on a file `.json` in the way I reported in the question – LJG Dec 02 '21 at 11:31
  • @LJG - Answer was edited. – jezrael Dec 02 '21 at 11:39
  • It gives me an error: AttributeError: 'list' object has no attribute 'dump' – LJG Dec 02 '21 at 11:47
  • @LJG - For me working perfectly, is possible upgrade python? – jezrael Dec 02 '21 at 11:49
  • Now it works. There was a variable called `json` in the previous part of the code. Do you know a way to print the list with the same formatting I used in the question? That is, to go to head every time it is required. – LJG Dec 02 '21 at 13:22
  • 1
    @LJG - maybe help [this](https://stackoverflow.com/questions/12943819/how-to-prettyprint-a-json-file) – jezrael Dec 02 '21 at 13:22
0
import json

data = {"data" : []}

for index, row in df.iterrows():
    data.append(row.to_dict())

with open('data.json', 'w') as fp:
    json.dump(data, fp)
Larry the Llama
  • 958
  • 3
  • 13