Python Forum

Full Version: Json to csv
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi ,

I am changing json to csv.

As below,

Input: json = {'name':'aaa', 'no':1, 'result1':['a','b','c'],'result2':['d','e'],'result3':[]}

output: csv
name no result grade
aaa 1 result1 a,b,c
aaa 1 result2 d,e
aaa 1 result3

please suggest me how do we get the csv file from the above json.
What have you tried? We'd love to help, but we're focused on helping people learn Python, not doing their work for them. Also, since this looks like homework I'm moving it to the homework section; please don't take it personally.
d3 = {'name':'aaa', 'no':1, 'result1':['a','b','c'],'result2':['d','e'],'result3':[]}

import csv

Doc = open('Desktop/doc.csv','w')
csvwriter = csv.writer(Doc)
count = 0
for d in d2:
if count == 0:
header = d2.keys()
csvwriter.writer(header)
count += 1
csvwriter.writerow(d2.values())
Doc.close()

Csv looks:
name no result1 result2 result3
aaa 1 [a,b,c] [d,e] []

We need to display below table.
name no result grade
aaa 1 result1 a,b,c
aaa 1 result2 d,e
aaa 1 result3
Please use proper code tags while posting your code.
d3 = {'name':'aaa', 'no':1, 'result1':['a','b','c'],'result2':['d','e'],'result3':[]}
print('name no result grade')
for d in d3.keys():
 if type(d3[d]) is list:
  print( d3['name'], d3['no'], d, ','.join(d3[d]))
Thank you. Updated the code. Still needs to add some of the implementation. Please feel free to post the solutions.
with open("C:/Temp/doc.csv","w") as s_doc:
  csvwriter = csv.writer(s_doc)
  header = ['name','no','result','grade']
  for d in d3.keys():
    if type(d3[d] is list:
     csvwriter.writerow([d3['name'],d3['no'],d,','.join(d3[d]))
[quote='pramod' pid='111294' dateline='1587428226']
d3 = {'name':'aaa', 'no':1, 'result1':['a','b','c'],'result2':['d','e'],'result3':[]}

import csv

Doc = open('Desktop/doc.csv','w')
csvwriter = csv.writer(Doc)
count = 0
for d in d2:
  if count == 0:
  header = d2.keys()
  csvwriter.writer(header)
  count += 1
  csvwriter.writerow(d2.values())
Doc.close()

Csv looks:
name no result1 result2 result3
aaa  1  [a,b,c]   [d,e]      []

We need to display below table.
name no result grade
aaa 1 result1  a,b,c
aaa 1 result2  d,e
aaa 1 result3
[/quote]

[python]
[/python]
(Apr-22-2020, 01:06 AM)pramod Wrote: [ -> ]Thank you. Updated the code. Still needs to add some of the implementation. Please feel free to post the solutions.
with open("C:/Temp/doc.csv","w") as s_doc:
  csvwriter = csv.writer(s_doc)
  header = ['name','no','result','grade']
  for d in d3.keys():
    if type(d3[d] is list:
     csvwriter.writerow([d3['name'],d3['no'],d,','.join(d3[d]))
Dont you get expected output from the above code?
(Apr-21-2020, 12:55 PM)anbu23 Wrote: [ -> ]
d3 = {'name':'aaa', 'no':1, 'result1':['a','b','c'],'result2':['d','e'],'result3':[]}
print('name no result grade')
for d in d3.keys():
 if type(d3[d]) is list:
  print( d3['name'], d3['no'], d, ','.join(d3[d]))

@anbu23, are you aware you just gave a code in the homework section? Have you even read the guidelines? You aren't supposed to post code for others, you are supposed to help them in finishing the code by giving advice
(Apr-24-2020, 10:33 AM)pyzyx3qwerty Wrote: [ -> ]Have you even read the guidelines?
I tend to be a stickler for this, but anbu23 did wait until the OP posted an attempt. I would have at least explained the changes, even if I were giving an answer (which I personally am less likely to do). In any case, anbu23 you're OK here I think but please do be mindful.
Pages: 1 2