I have the following JSON file:
[
{
"Names": {
"0": "Nat",
"1": "Harry",
"2": "Joe"
},
"Marks": {
"0": 78.22,
"1": 32.54,
"2": 87.23
}
}
]
I have written the following code for conversion:
import csv, json
def conversion(Jsonfile,Csvfile):
readfile=open(Jsonfile,"r")
print(readfile)
jsondata=json.load(readfile)
print(jsondata)
readfile.close()
data_file=open(Csvfile,'w')
csv_writer=csv.writer(data_file)
count=0
for data in jsondata:
if count==0:
header=data.keys()
print(header)
csv_writer.writerow(header)
count=count+1
csv_writer.writerow(data.values())
print(data.values())
data_file.close()
Jsonfile="Series.json"
Csvfile="convertedfile.csv"
conversion(Jsonfile,Csvfile)
I am getting the following output in CSV
Names,Marks
"{'0': 'Nat', '1': 'Harry', '2': 'Joe'}","{'0': 78.22, '1': 32.54, '2': 87.23}"
My question is how to correct the code to get the following output ( that is each name with marks in a different line):
Names,Marks
0,Nat,78.22
1,Harry,32.54
2,Joe,87.23