使用Python将JSON解析为CSV:AttributeError:'unicode'对象没有属性'keys'

时间:2017-06-06 03:36:15

标签: python json csv nested

我有一个嵌套的JSON数据集,其中包含多个条目,如下所示:

{
"coordinates": null,
"acoustic_features": {
    "instrumentalness": "0.00479",
    "liveness": "0.18",
    "speechiness": "0.0294",
    "danceability": "0.634",
    "valence": "0.342",
    "loudness": "-8.345",
    "tempo": "125.044",
    "acousticness": "0.00035",
    "energy": "0.697",
    "mode": "1",
    "key": "6"
},
"artist_id": "b2980c722a1ace7a30303718ce5491d8",
"place": null,
"geo": null,
"tweet_lang": "en",
"source": "Share.Radionomy.com",
"track_title": "8eeZ",
"track_id": "cd52b3e5b51da29e5893dba82a418a4b",
"artist_name": "Dominion",
"entities": {
    "hashtags": [{
        "text": "nowplaying",
        "indices": [0, 11]
    }, {
        "text": "goth",
        "indices": [51, 56]
    }, {
        "text": "deathrock",
        "indices": [57, 67]
    }, {
        "text": "postpunk",
        "indices": [68, 77]
    }],
    "symbols": [],
    "user_mentions": [],
    "urls": [{
        "indices": [28, 50],
        "expanded_url": "cathedral13.com/blog13",
        "display_url": "cathedral13.com/blog13",
        "url": "t.co/Tatf4hEVkv"
    }]
},
"created_at": "2014-01-01 05:54:21",
"text": "#nowplaying Dominion - 8eeZ Tatf4hEVkv #goth #deathrock #postpunk",
"user": {
    "location": "middle of nowhere",
    "lang": "en",
    "time_zone": "Central Time (US & Canada)",
    "name": "Cathedral 13",
    "entities": null,
    "id": 81496937,
    "description": "I\u2019m a music junkie who is currently responsible for Cathedral 13 internet radio (goth, deathrock, post-punk)which has been online since 06/20/02."
},
"id": 418243774842929150
}

我想将其转换为csv文件,其中有多个列包含每个JSON对象的相应条目。以下是我编写的Python代码:

import json
import csv
from pprint import pprint
data = []
with open('data_subset.json') as data_file:
    for line in data_file:
        data.append(json.loads(line))

# open a file for writing
data_csv = open('Data_csv.csv', 'w')
# create the csv writer object
csvwriter = csv.writer(data_csv)

for i in range(1,10):
    count = 0
    for dat in data[i]:
        if count == 0:
             header = dat.keys()
             csvwriter.writerow(header)
             count += 1
        csvwriter.writerow(emp.values())
data_csv.close()

在运行上面的代码时,我收到错误:AttributeError:'unicode'对象没有属性'keys'。 可能是什么问题?

1 个答案:

答案 0 :(得分:2)

您可以一次性阅读JSON文件,如:

with open('a.txt') as data_file:    
    data = json.load(data_file)

现在您将JSON作为data字典。

由于您需要从JSON到csv的特定条目(例如entities未保存到csv),您可以保留自定义列标题,然后循环数据以将特定键写入csv:

# Example to save the artist_id and user id; can be extended for the actual data
header = ['artist_id', 'id']

# open a file for writing
data_csv = open('Data_csv.csv', 'wb')

# create the csv writer object
csvwriter = csv.writer(data_csv)

# write the csv header
csvwriter.writerow(header)

for entry in data:
    csvwriter.writerow([entry['artist_id'], entry['user']['id']])

data_csv.close()