Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Json to csv
#1
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.
Reply
#2
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.
Reply
#3
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
Reply
#4
Please use proper code tags while posting your code.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#5
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]))
Reply
#6
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]))
Reply
#7
[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]
Reply
#8
(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?
Reply
#9
(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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#10
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020