![]() |
Json to csv - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Json to csv (/thread-26085.html) Pages:
1
2
|
Json to csv - pramod - Apr-20-2020 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. RE: Json to csv - micseydel - Apr-20-2020 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. RE: Json to csv - pramod - Apr-21-2020 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 RE: Json to csv - pyzyx3qwerty - Apr-21-2020 Please use proper code tags while posting your code. RE: Json to csv - anbu23 - Apr-21-2020 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])) RE: Json to csv - pramod - Apr-22-2020 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])) RE: Json to csv - pramod - Apr-23-2020 [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] RE: Json to csv - anbu23 - Apr-24-2020 (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.Dont you get expected output from the above code? RE: Json to csv - pyzyx3qwerty - Apr-24-2020 (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 RE: Json to csv - micseydel - Apr-24-2020 (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. |