Python将JSON转换为CSV [英] Python convert JSON to CSV

查看:129
本文介绍了Python将JSON转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件,其中包含:

i have a JSON file which contains:

{
    "leaderboard": {
        "$": [
            {
                "userId": 1432024286216,
                "userName": "Joe Bloggs",
                "score": 111111,
                "gameType": "standard",
                "dateCreated": 1432024397833,
                "_id": 1432024397833
            },
            {
                "userId": 1432024626556,
                "userName": "Jane Bloggs",
                "score": 222222,
                "gameType": "demo",
                "dateCreated": 1432024730861,
                "_id": 1432024730861
            }
        ]
    },
    "users": {
        "$": [
            {
                "userId_U": 1432024286000,
                "userName_U": "Paul Bloggs",
                "score_U": 333333,
                "gameType_U": "standard",
                "dateCreated_U": 1432024397833,
                "_id_U": 1432024397833
            },
            {
                "userId_U": 1432024626777,
                "userName_U": "John Bloggs",
                "score_U": 444444,
                "gameType_U": "demo",
                "dateCreated_U": 1432024730861,
                "_id_U": 1432024730861
            }
        ]
    }
}

我正在尝试用Python创建一个CSV文件. CSV仅从排行榜"数据对象创建标头:userId,userName等,并为其填充相应的数据.因此,分别为userId,userName等创建一列.

i am trying to create a CSV from this in Python. The CSV creates the headers: userId, userName etc. only from the 'leaderboard' data object only and populate the corresponding data for it. so create a column each for: userId, userName etc.

我开始对此进行编码,但是我正在创建排行榜"和用户"标头,并将它们的数据存储在其下方的1个单元格中. 我的代码:

i started coding this but i am getting the 'leaderboard' and 'users' headers created and their data in 1 cell beneath them. my code:

import json, csv

x = open('test/dbTest.json')

rows = json.load(x)
with open('test.csv', 'wb+') as f:
    dict_writer = csv.DictWriter(f, fieldnames=['leaderboard', 'users'])
    dict_writer.writeheader()
    dict_writer.writerow(rows)

我尝试将字段名称更改为'userId','userName'等,但随后出现错误:ValueError: dict contains fields not in fieldnames: u'users', u'leaderboard'

i have tried to change the field name to 'userId' , 'userName' etc but it then gives error: ValueError: dict contains fields not in fieldnames: u'users', u'leaderboard'

有人可以帮助您提取我需要的数据吗?解释为什么上面的代码不正确,请指出正确的方向? 道歉,如果不清楚,如果缺少任何内容.

can someone please help how to get extract the data i need? explanation why above code is incorrect and kindly point me in the right direction please? apologies if not clear and if missing anything.

此外,CSV也应如下所示:

also, the CSV should look like:

userId,userName,score,gameType,dateCreated,_id,
1432024286216,Joe Bloggs,111111,standard,1432024397833,1432024397833
1432024626556,Jane Bloggs,222222,demo,1432024730861,1432024730861

并且需要澄清的是,用户"和排行榜"的字段名称不同.

and to clarify, 'users' and 'leaderboard' are different with different field names.

推荐答案

# json_data being the literal file data, in this example

import json
import csv

data = json.loads(json_data)['leaderboard']['$']

with open('/tmp/test.csv', 'w') as outf:
    dw = csv.DictWriter(outf, data[0].keys())
    dw.writeheader()
    for row in data:
        dw.writerow(row)

这篇关于Python将JSON转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆