python - Python中的json到csv转换返回单行?

标签 python json csv

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

下面是我的 json 文件的片段。它具有“类(class)”的嵌套内容,其中一些类(class)值存在,而另一些则为空。

[{
    "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

最佳答案

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

...

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)

 ...

关于python - Python中的json到csv转换返回单行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33749022/

相关文章:

python - 如何在 Windows 中的 Python 3 中制定端口转发规则?

python - 在读取或写入文件系统时执行其他操作

python - 无法使用 django 修改数据库

python - 如何使用字符编码在文件中存储随机字节?

javascript - 嵌套循环花费的时间太长,几乎使浏览器崩溃

ios - dataWithContentsOfMappedFile 在 iOS 8.0 中弃用

ruby-on-rails - Rails 5 - 如何将 Controller 中整个 jsonb postgres 列的参数列入白名单?

python - 将元数据/标识符数据添加到 CSV 文件?

python - CSV文件解析(python)

python - 如何从旧数据框创建新数据框?