json to csv在Python中转换返回单行?

时间:2015-11-17 04:11:11

标签: python json csv

我最近从stackoverflow获得了帮助并更正了我的脚本以将json转换为csv。但是,现在脚本只返回一行,请在下面的代码中建议修改,以便我可以将整个json文件转换为csv。

以下是我的json文件的片段。它具有“课程”的嵌套内容,其中一些课程值存在,而某些课程值为空。

[{
    "address": " Vidyanagar, Hubli-580031", 
    "college": "College (Architecture)", 
    "courses": [], 
    "email": " principal@bvb.edu", 
    "fax": "0836-2374985", 
    "name": "School Of Architecturebv Bhoomaradi College Of Engg. & Technology", 
    "phone": "0836-2378123, 2378201", 
    "recognition": " V.t.u. Belgaum", 
    "website": ""
},{
    "address": " Udyambag, Belgaum-590008", 
    "college": "Institute (Architecture)", 
    "courses": [], 
    "email": " principal@git.edu", 
    "fax": "0831-2441909", 
    "name": "School Of Architecturelaw Society's Gogte Institute Of Technology", 
    "phone": "0831-2441104, 2405507", 
    "recognition": " V.t.u. Belgaum", 
    "website": ""
},{
    "address": " Vidya Southa, Gokula Extn. Post, Bantwal-560054", 
    "college": "Institute (Architecture)", 
    "courses": [], 
    "email": " hod_at@msrit.edu", 
    "fax": "080-23603124", 
    "name": "School Of Architecturems Ramaiah Institute Of Technology ", 
    "phone": "080-23606934, 23600822", 
    "recognition": " V.t.u. Belgaum", 
    "website": ""
},{
    "address": " -, Gulbarga-585102", 
    "college": "College (Architecture)", 
    "courses": [
        {
            "brief_details": "", 
            "college_name": "School of ArchitecturePoojya Doddappa Appa College of Engineering", 
            "course_branch": "B.Arch", 
            "course_duration": " 5-year", 
            "course_nature": " Full-Time", 
            "course_title": "", 
            "course_type": " B.Arch", 
            "no_of_seats": " 60", 
            "qualifications": "", 
            "selection_process": ""
        }
    ], 
    "email": " principal@pdaengg.com", 
    "fax": "08472-255685", 
    "name": "School Of Architecturepoojya Doddappa Appa College Of Engineering", 
    "phone": "08472-224262 Extn. 435, 220742", 
    "recognition": " V.t.u. Belgaum", 
    "website": ""
},{
    "address": " R.v. Vidyaniketan P.o., Mysore Road, Bangalore-560059", 
    "college": "College (Architecture)", 
    "courses": [
        {
            "brief_details": "", 
            "college_name": "School of ArchitectureR.V. College of Engineering", 
            "course_branch": "B.Arch", 
            "course_duration": " 5-year", 
            "course_nature": " Full-Time", 
            "course_title": "", 
            "course_type": " B.Arch", 
            "no_of_seats": " 20", 
            "qualifications": "", 
            "selection_process": ""
        }
    ], 
    "email": " architecture@rvce.ac.in", 
    "fax": "080-28602914, 28602148, 28600337", 
    "name": "School Of Architecturer.v. College Of Engineering", 
    "phone": "080-28602170, 28601258, 28600184", 
    "recognition": " V.t.u. Belgaum", 
    "website": "www.rvce.ac.in"
}

以下是我的代码:

import json
import csv

def write_csv(jsonfile, outfile):

    with open(jsonfile) as f:
        data = json.loads(f.read())

    college_dict = data[0]

    college_keys = list(college_dict.keys())
    college_keys.remove('courses')
    college_keys.remove('college')

    courses_dict = data[0]['courses'][0] if data[0]['courses'] else {'brief_details' : None}
    courses_keys = list(courses_dict.keys())
    courses_keys.remove('brief_details')

    with open(outfile, 'wb') as f:
        csv_writer = csv.writer(f)
        headers = college_keys + courses_keys
        csv_writer.writerow(headers)

        row = (
            [
                college_dict[key] if college_dict[key] else 'NA'
                for key in college_keys
            ] 
            + 
            [
                courses_dict[key] if courses_dict[key] else 'NA'
                for key in courses_keys
            ]
        )

        csv_writer.writerow(row)

jsonfile = '/home/maitreyee/Downloads/SchoolCollege.com/collegesdb/collegesdb1.json'
outfile = '/home/maitreyee/Downloads/SchoolCollege.com/collegesdb/collegesout.csv'

write_csv(jsonfile, outfile)

这个json文件很大,但下面是生成的csv:

website,fax,name,phone,address,email,recognition
NA,0836-2374985,School Of Architecturebv Bhoomaradi College Of Engg. & Technology,"0836-2378123, 2378201"," Vidyanagar, Hubli-580031", principal@bvb.edu, V.t.u. Belgaum

1 个答案:

答案 0 :(得分:1)

当前代码仅转换第一项data[0]。您需要迭代data

...

with open(outfile, 'wb') as f:
    csv_writer = csv.writer(f)
    headers = college_keys + courses_keys
    csv_writer.writerow(headers)

    for d in data:
        row = (
            [d[key] or 'NA' for key in college_keys] +
            [d[key] or 'NA' for key in courses_keys]
        )
        csv_writer.writerow(row)

 ...