从嵌套的JSON文件中提取文本,其中每个JSON对象在Python

时间:2017-07-07 07:23:29

标签: python json

我有一个包含多个嵌套json对象的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文件。 我编写了以下代码来执行此操作:

import csv
with open('jsonpart.json') as data_file:
    data = json.load(data_file)
    #print (data)
    header = ['hashtags']

# open a file for writing
data_csv = open('hashtags.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['entities']['hashtags']])

data_csv.close()

我得到以下输出文件:

"[{u'indices': [0, 11], u'text': u'nowplaying'}, {u'indices': [51, 56], 
 u'text': u'goth'}, {u'indices': [57, 67], u'text': u'deathrock'}, 
{u'indices': [68, 77], u'text': u'postpunk'}]"
"[{u'indices': [23, 34], u'text': u'NowPlaying'}, {u'indices': [75, 79], 
u'text': u'80s'}, {u'indices': [80, 86], u'text': u'Retro'}, {u'indices': 
[87, 91], u'text': u'Fun'}]"
"[{u'indices': [0, 11], u'text': u'nowplaying'}]"
"[{u'indices': [54, 65], u'text': u'nowplaying'}, {u'indices': [66, 77], 
u'text': u'listenlive'}]"

我被困在这里。如何将目标文件作为:

nowplaying
goth
deathrock
postpunk
NowPlaying  
80's
Retro
Fun
nowplaying
nowplaying
listenlive

1 个答案:

答案 0 :(得分:1)

您可以使用简单的列表理解。假设您有一个名为json_chunk的json对象,您可以像这样创建列表:

{ "adid": 5, "uid": 1130, "userName": "Maëly", "cid": 5, "brand": null, "model": null, "title": "Nummular dermatitis", "description": "Nam sa dea ss " "address": "9795 Debra Center", "price": 1544, "photo1": "../../../img/mobile/oneplus/3t/3t-1.jpg", "photo2": null, "photo3": null, "photo4": null, "ip_add": null, "created": 1493921700000, "expiryDate": null, "published": true, "sold": null, "featured": true, "cond": true, "price_neg": true, "used_for": "8", "views": 1575, "city": "Kathmandu", "landmark": null, "quality": null, "alert": null, "features": [ ] } ]

现在你有了一个清单。迭代它(某些元素显然有一个新行字符别人 - 所以剥离所有并为所有人添加新行字符)并将每个元素写入文件,如下所示:

text_list = [hashtag['text'] for hashtag in json_chunk['entities']['hashtags']