2017-05-03 88 views
0

我用下面的代碼初學者:打印「井號標籤」從JSON到CSV文件使用Python

f = open("singletweetwithtimezone.json", "r") 
tweet_text = f.read() 

import json 

tweet_json = json.loads(tweet_text) 

g = open("Singletweetcsvoutput.csv", "w") 

g.write(tweet_json["created_at"]+"\t") 
g.write(tweet_json["user"]["time_zone"]+"\t") 
g.write(tweet_json["entities"]["hashtags"]["text"]) 

g.close() 
f.close() 

寫作的作品,除了井號標籤。我希望它能夠在CSV文件中編寫文本'messi',但由於缺乏知識,我無法弄清楚我做錯了什麼。我得到以下錯誤:

g.write(tweet_json["entities"]["hashtags"]["text"]) 
TypeError: list indices must be integers, not str". " 

的JSON樹圖所示我加: what the tree looks like

任何人誰可以幫我嗎?

RAW JSON代碼:

{"created_at":"Sun Apr 23 21:04:13 +0000 2017","id":856252394233106432,"id_str":"856252394233106432","text":"RT @11FC_FR: Et \u00e0 la fin ... #Messi \n\ud83d\ude0d https:\/\/t.co\/uiyTnJJiKd","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1628829458,"id_str":"1628829458","name":"LK20","screen_name":"KeevinGruny","location":null,"url":null,"description":"\u26bd\ufe0f\u26bd\ufe0f","protected":false,"verified":false,"followers_count":903,"friends_count":209,"listed_count":39,"favourites_count":7328,"statuses_count":59480,"created_at":"Sun Jul 28 21:50:55 +0000 2013","utc_offset":10800,"time_zone":"Athens","geo_enabled":true,"lang":"fr","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/854838667428466688\/jE52U_LU_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/854838667428466688\/jE52U_LU_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1628829458\/1492857072","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sun Apr 23 20:38:37 +0000 2017","id":856245950544838660,"id_str":"856245950544838660","text":"Et \u00e0 la fin ... #Messi \n\ud83d\ude0d https:\/\/t.co\/uiyTnJJiKd","display_text_range":[0,25],"source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":2617563403,"id_str":"2617563403","name":"11FootballClub","screen_name":"11FC_FR","location":"3, All\u00e9e Cassard - NANTES","url":"http:\/\/www.11footballclub.com","description":"11FootballClub est un concept store unique et une boutique en ligne soign\u00e9e. Actus foot, nouveaut\u00e9s produits, promos et jeux concours","protected":false,"verified":false,"followers_count":49693,"friends_count":24561,"listed_count":54,"favourites_count":350,"statuses_count":1268,"created_at":"Fri Jul 11 15:21:20 +0000 2014","utc_offset":-25200,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"lang":"fr","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"A16E1E","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/648580532322836480\/2lodFucd_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/648580532322836480\/2lodFucd_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/2617563403\/1443468740","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":155,"favorite_count":92,"entities":{"hashtags":[{"text":"Messi","indices":[16,22]}],"urls":[],"user_mentions":[],"symbols":[],"media":[{"id":856245937915793408,"id_str":"856245937915793408","indices":[26,49],"media_url":"http:\/\/pbs.twimg.com\/media\/C-H_JNnXkAApCFh.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/C-H_JNnXkAApCFh.jpg","url":"https:\/\/t.co\/uiyTnJJiKd","display_url":"pic.twitter.com\/uiyTnJJiKd","expanded_url":"https:\/\/twitter.com\/11FC_FR\/status\/856245950544838660\/photo\/1","type":"photo","sizes":{"small":{"w":680,"h":460,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":1200,"h":812,"resize":"fit"},"large":{"w":2048,"h":1386,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":856245937915793408,"id_str":"856245937915793408","indices":[26,49],"media_url":"http:\/\/pbs.twimg.com\/media\/C-H_JNnXkAApCFh.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/C-H_JNnXkAApCFh.jpg","url":"https:\/\/t.co\/uiyTnJJiKd","display_url":"pic.twitter.com\/uiyTnJJiKd","expanded_url":"https:\/\/twitter.com\/11FC_FR\/status\/856245950544838660\/photo\/1","type":"photo","sizes":{"small":{"w":680,"h":460,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":1200,"h":812,"resize":"fit"},"large":{"w":2048,"h":1386,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"fr"},"is_quote_status":false,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"Messi","indices":[29,35]}],"urls":[],"user_mentions":[{"screen_name":"11FC_FR","name":"11FootballClub","id":2617563403,"id_str":"2617563403","indices":[3,11]}],"symbols":[],"media":[{"id":856245937915793408,"id_str":"856245937915793408","indices":[39,62],"media_url":"http:\/\/pbs.twimg.com\/media\/C-H_JNnXkAApCFh.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/C-H_JNnXkAApCFh.jpg","url":"https:\/\/t.co\/uiyTnJJiKd","display_url":"pic.twitter.com\/uiyTnJJiKd","expanded_url":"https:\/\/twitter.com\/11FC_FR\/status\/856245950544838660\/photo\/1","type":"photo","sizes":{"small":{"w":680,"h":460,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":1200,"h":812,"resize":"fit"},"large":{"w":2048,"h":1386,"resize":"fit"}},"source_status_id":856245950544838660,"source_status_id_str":"856245950544838660","source_user_id":2617563403,"source_user_id_str":"2617563403"}]},"extended_entities":{"media":[{"id":856245937915793408,"id_str":"856245937915793408","indices":[39,62],"media_url":"http:\/\/pbs.twimg.com\/media\/C-H_JNnXkAApCFh.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/C-H_JNnXkAApCFh.jpg","url":"https:\/\/t.co\/uiyTnJJiKd","display_url":"pic.twitter.com\/uiyTnJJiKd","expanded_url":"https:\/\/twitter.com\/11FC_FR\/status\/856245950544838660\/photo\/1","type":"photo","sizes":{"small":{"w":680,"h":460,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":1200,"h":812,"resize":"fit"},"large":{"w":2048,"h":1386,"resize":"fit"}},"source_status_id":856245950544838660,"source_status_id_str":"856245950544838660","source_user_id":2617563403,"source_user_id_str":"2617563403"}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"fr","timestamp_ms":"1492981453842"} 
+0

檢查更新後的答案並進行測試。 – ciacicode

回答

0

開始通過印刷在響應中的每個不同的節點的類型。您看到的錯誤是由於您嘗試訪問響應中的所有內容而造成的,因爲它是字典的關鍵。的事情是,主題標籤,從截圖,可能是陣列,因此需要作爲待接入:

tweet_json['entities']['hashtags'][0]['text'] 

主題標籤包含一個數組,在這種情況下,陣列的長度爲1,因此你與訪問[ 0],但由於這些數組的長度是可變的,你應該添加一個長度檢查,然後做一個如下所示的循環。我喜歡使用來自csv library的dictwriter,即使這是一個過度殺戮,它可以用來通過多個推文。

import csv 
import json 


with open('.../input.json','r') as inputfile: 
    tweet= inputfile.read() 

tweet_json = json.loads(tweet) 

with open('.../output.csv', 'w') as csvfile: 
    fieldnames = ['created_at', 'user', 'hashtags'] 
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames) 
    writer.writeheader() 
    #extract all info that you want to write 
    created_at = tweet_json['created_at'] 
    #selecting the screen_name of the user rather than id 
    user = tweet_json['user']['screen_name'] 
    hashtags = tweet_json['entities']['hashtags'] 
    #creating an empty string for the hashtags in the array 
    hashes = list() 
    for hashtag in hashtags: 
     text = hashtag['text'] 
     #append to hashes listed_count 
     hashes.append(text) 
    #stringify the list and write to file (will be ugly) 
    writer.writerow({"created_at":created_at, "user":user,"hashtags":str(hashes) }) 
+0

謝謝,這裏是原始代碼,添加到問題中。 – MaroYKW

+0

感謝您的更新!這將如何使完整的代碼看起來像,我不能得到它的工作:')。 – MaroYKW

+0

@MaroYKW檢查一下 – ciacicode