我在使用python将JSON转换为CSV时遇到问题

时间:2017-11-11 15:16:40

标签: python json csv

我有这个JSON文件,我试图转换为CSV来收集数据。然而,我得到的输出远非正确。

到目前为止,我有:

{
    "info": "SQLite Pro Result Set",
    "data": [
        {
            "top40_SK": "118899",
            "song_title": "Sorry",
            "artist": "Justin Bieber",
            "year_released": "2015",
            "year": "2016",
            "week": "1",
            "position": "1",
            "prev_position": "1",
            "weeks_in_top40_v1": "10",
            "weeks_in_top40_v2": "10",
            "highest_reached_position": "1",
            "total_points": "775",
            "top40url": "https://www.top40.nl/top40/2016/week-1"
        },
        {
            "top40_SK": "118900",
            "song_title": "Love yourself",
            "artist": "Justin Bieber",
            "year_released": "2015",
            "year": "2016",
            "week": "1",
            "position": "2",
            "prev_position": "2",
            "weeks_in_top40_v1": "6",
            "weeks_in_top40_v2": "6",
            "highest_reached_position": "1",
            "total_points": "764",
            "top40url": "https://www.top40.nl/top40/2016/week-1"
        }
    ]
}

使用这种类型的JSON数据(例如):

i,n,f,o

到目前为止所有上述工作都有效,但这是我的输出:     d,a,t,a     {{1}}

知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

我不明白为什么你想要一个CSV文件而不是JSON,但是在这里你可以从每个数据列表中提取dicts并将它们写入CSV。为了简化示例,我只将输出写入sys.stdout而不是磁盘文件。

import json
import csv
import sys

JSON = '''\
{
    "info": "SQLite Pro Result Set",
    "data": [
        {
            "top40_SK": "118899",
            "song_title": "Sorry",
            "artist": "Justin Bieber",
            "year_released": "2015",
            "year": "2016",
            "week": "1",
            "position": "1",
            "prev_position": "1",
            "weeks_in_top40_v1": "10",
            "weeks_in_top40_v2": "10",
            "highest_reached_position": "1",
            "total_points": "775",
            "top40url": "https://www.top40.nl/top40/2016/week-1"
        },
        {
            "top40_SK": "118900",
            "song_title": "Love yourself",
            "artist": "Justin Bieber",
            "year_released": "2015",
            "year": "2016",
            "week": "1",
            "position": "2",
            "prev_position": "2",
            "weeks_in_top40_v1": "6",
            "weeks_in_top40_v2": "6",
            "highest_reached_position": "1",
            "total_points": "764",
            "top40url": "https://www.top40.nl/top40/2016/week-1"
        }
    ]
}
'''

data = json.loads(JSON)

keys = data["data"][0].keys()
writer = csv.DictWriter(sys.stdout, fieldnames=keys)
writer.writerow(dict(zip(keys, keys)))
for d in data["data"]:
    writer.writerow(d)

<强>输出

top40_SK,song_title,artist,year_released,year,week,position,prev_position,weeks_in_top40_v1,weeks_in_top40_v2,highest_reached_position,total_points,top40url
118899,Sorry,Justin Bieber,2015,2016,1,1,1,10,10,1,775,https://www.top40.nl/top40/2016/week-1
118900,Love yourself,Justin Bieber,2015,2016,1,2,2,6,6,1,764,https://www.top40.nl/top40/2016/week-1

答案 1 :(得分:0)

这里使用pandas库

是最简单的
import pandas as pd
import json

with open('top40nl.json') as fi:
    data = json.load(fi)
    df = pd.DataFrame(data=data)
    df.to_csv('top40nl.csv', index=False)