从文件列表中加载逗号分隔的dict

时间:2017-05-17 12:29:45

标签: python json twitter

我使用以下代码将json文件中的数据附加到文本文件中:

list=['C:/Users/Desktop/datasets/580317556516483072/source-
tweet/580317556516483072.json', 
'C:/Users/Desktop/datasets/580317998147325952/source-
tweet/580317998147325952.json', 
..........]
file=open('C:/Users/Desktop/proj/datasets/del.txt',"a")      
file.write("[\n")
first=True
for i in range(list(s3)):
    if first:
    first=False
    else:
       file.write(",\n")
    with open(list[i]) as f:
       u=json.load(f)
       s=json.dumps(u)
       file.write(s)
 file.write("]\n")
 file.close()

文本文件中的数据如下所示:

[
{"retweeted": false, "favorited": false, "in_reply_to_user_id": null, "source": "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>", "favorite_count": 14, "in_reply_to_user_id_str": null, "user": {"geo_enabled": false, "friends_count": 246, "profile_images/1576508322/Henry_Samuel_normal.jpg", "followers_count": 1628, "is_translation_enabled": false, "default_profile_image": false, "is_translator": false, "following": false, "profile_text_color": "333333", "follow_request_sent": false, "listed_count": 78, "contributors_enabled": false, "profile_background_color": "C0DEED", "description": "Paris correspondent, The Daily Telegraph. All views expressed are mine only.", "created_at": "Tue Oct 04 09:36:17 +0000 2011", "profile_use_background_image": true, "profile_background_tile": false, "name": "Henry Samuel", "notifications": false, "screen_name": "H_E_Samuel", "default_profile": true, "verified": false, "id_str": "384779793", "profile_sidebar_fill_color": "DDEEF6", "favourites_count": 5, "time_zone": "Paris", "entities": {"description": {"urls": []}, "url": {"urls": [{"indices": [0, 22], "display_url": "telegraph.co.uk/journalists/he\u2026", "expanded_url": "http://www.telegraph.co.uk/journalists/henry-samuel/"}]}}, "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_location": null, "utc_offset": 3600, "profile_link_color": "0084B4", "statuses_count": 1901, "id": 384779793, "lang": "en", "protected": false, "location": "Paris", "profile_sidebar_border_color": "C0DEED"}, "created_at": "Wed Jan 07 11:06:08 +0000 2015", "place": null, "in_reply_to_status_id": null, "geo": null, "in_reply_to_screen_name": null, "id_str": "552783238415265792", "entities": {"hashtags": [], "symbols": [], "urls": [], "user_mentions": []}, "contributors": null, "in_reply_to_status_id_str": null, "coordinates": null, "retweet_count": 159, "text": "Breaking: At least 10 dead, 5 injured after tO gunman open fire in offices of Charlie  Hebdo,satirical mag that published Mohammed cartoons", "id": 552783238415265792, "lang": "en", "truncated": false},
]

当我尝试使用以下方法从文件加载数据时

with open(path to file) as f:
    for each in f:
        print(each['id'])

我收到错误: &#34;字符串索引必须是整数&#34;

如何加载当前为字符串格式的字典文件?

1 个答案:

答案 0 :(得分:2)

它将输入文件作为字符串读取。为了使其成为列表或词典,您需要执行以下操作:

from json import loads

with open(path_to_file) as f:
    for each in loads(f.read()):
        print(each['id'])

根据json.loads文档字符串,它用于“反序列化...(包含JSON文档的'str'或'unicode'实例)到Python对象。”