如何读取json文件中有几个json对象并在jupyter笔记本中提取值?

时间:2018-03-15 23:11:40

标签: python json parsing

我有这个json文件:

{"created_at":"Thu Mar 15 20:05:53 +0000 2018","id":974376156186206208,"id_str":"974376156186206208","text":"RT @dtrubchenkov: #eth_town #ethereum #game #crypto\nLast but mandatory, you have to follow their Twitter account: https:\/\/t.co\/rjjvVStl6z.\u2026","source":"\u003ca href=\"https:\/\/discoveringmypath.wixsite.com\/discoveringmypath\" rel=\"nofollow\"\u003ereadysetgame\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":969005342125371393,"id_str":"969005342125371393","name":"ReadySetGame","screen_name":"ReadySetGame3","location":null,"url":null,"description":"Ready set game. Created by @Discovermypath","translator_type":"none","protected":false,"verified":false,"followers_count":266,"friends_count":1,"listed_count":14,"favourites_count":0,"statuses_count":16543,"created_at":"Thu Mar 01 00:24:11 +0000 2018","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"F5F8FA","profile_background_image_url":"","profile_background_image_url_https":"","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\/969014017988546561\/oTXMTMQD_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/969014017988546561\/oTXMTMQD_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/969005342125371393\/1519865955","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":"Mon Mar 12 15:44:38 +0000 2018","id":973223247746609152,"id_str":"973223247746609152","text":"#eth_town #ethereum #game #crypto\nLast but mandatory, you have to follow their Twitter account:\u2026 https:\/\/t.co\/vFhvjsQGZA","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e","truncated":true,"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":956518734189006851,"id_str":"956518734189006851","name":"\u0414\u0438\u043c\u0430 \u0422\u0440\u0443\u0431\u0447\u0435\u043d\u043a\u043e\u0432","screen_name":"dtrubchenkov","location":null,"url":null,"description":"\u0412\u0435\u0441\u0435\u043b\u044b\u0439, \u043e\u0431\u0449\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0438 \u0436\u0438\u0437\u043d\u0435\u0440\u0430\u0434\u043e\u0441\u0442\u043d\u044b\u0439","translator_type":"none","protected":false,"verified":false,"followers_count":1817,"friends_count":2062,"listed_count":5,"favourites_count":1500,"statuses_count":1624,"created_at":"Thu Jan 25 13:26:52 +0000 2018","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"ru","contributors_enabled":false,"is_translator":false,"profile_background_color":"F5F8FA","profile_background_image_url":"","profile_background_image_url_https":"","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\/956519643400765441\/adZtSbo3_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/956519643400765441\/adZtSbo3_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/956518734189006851\/1519292876","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"extended_tweet":{"full_text":"#eth_town #ethereum #game #crypto\nLast but mandatory, you have to follow their Twitter account: https:\/\/t.co\/rjjvVStl6z. And you must like and follow their Facebook page: https:\/\/t.co\/fcQ3EyaFXa. The team will verify each subscription\u2026","display_text_range":[0,235],"entities":{"hashtags":[{"text":"eth_town","indices":[0,9]},{"text":"ethereum","indices":[10,19]},{"text":"game","indices":[20,25]},{"text":"crypto","indices":[26,33]}],"urls":[{"url":"https:\/\/t.co\/rjjvVStl6z","expanded_url":"https:\/\/twitter.com\/eth_town","display_url":"twitter.com\/eth_town","indices":[96,119]},{"url":"https:\/\/t.co\/fcQ3EyaFXa","expanded_url":"https:\/\/fb.me\/eth.town","display_url":"fb.me\/eth.town","indices":[171,194]}],"user_mentions":[],"symbols":[]}},"quote_count":0,"reply_count":0,"retweet_count":1,"favorite_count":0,"entities":{"hashtags":[{"text":"eth_town","indices":[0,9]},{"text":"ethereum","indices":[10,19]},{"text":"game","indices":[20,25]},{"text":"crypto","indices":[26,33]}],"urls":[{"url":"https:\/\/t.co\/vFhvjsQGZA","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/973223247746609152","display_url":"twitter.com\/i\/web\/status\/9\u2026","indices":[97,120]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"eth_town","indices":[18,27]},{"text":"ethereum","indices":[28,37]},{"text":"game","indices":[38,43]},{"text":"crypto","indices":[44,51]}],"urls":[{"url":"https:\/\/t.co\/rjjvVStl6z","expanded_url":"https:\/\/twitter.com\/eth_town","display_url":"twitter.com\/eth_town","indices":[114,137]}],"user_mentions":[{"screen_name":"dtrubchenkov","name":"\u0414\u0438\u043c\u0430 \u0422\u0440\u0443\u0431\u0447\u0435\u043d\u043a\u043e\u0432","id":956518734189006851,"id_str":"956518734189006851","indices":[3,16]}],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1521144353325"}

{"created_at":"Thu Mar 15 20:05:53 +0000 2018","id":974376157247393792,"id_str":"974376157247393792","text":"Hoje eu vou deixar como p\u00fablico.\nhttps:\/\/t.co\/f3Nr8ZjYPV\n#PTBR #game #indie #gameplay #VideoGame #VisualNovel\u2026 https:\/\/t.co\/KJaWnStVyB","display_text_range":[0,140],"source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e","truncated":true,"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":1663346617,"id_str":"1663346617","name":"Player Banzai","screen_name":"PlayerBanzai","location":null,"url":null,"description":"Ol\u00e1 pessoal! Meu canal de games foca mais nos jogos de RPG Makers, de terror e de anime, mas tamb\u00e9m posto de outros estilos. Posto tamb\u00e9m quiz dos mesmos.","translator_type":"none","protected":false,"verified":false,"followers_count":165,"friends_count":129,"listed_count":2,"favourites_count":1038,"statuses_count":8521,"created_at":"Sun Aug 11 19:48:14 +0000 2013","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"pt","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/378800000049385411\/cc967c6303c376407da9f249a0f63ab4.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/378800000049385411\/cc967c6303c376407da9f249a0f63ab4.jpeg","profile_background_tile":false,"profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/953299057228898305\/ARtkim0P_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/953299057228898305\/ARtkim0P_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1663346617\/1516068748","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,"extended_tweet":{"full_text":"Hoje eu vou deixar como p\u00fablico.\nhttps:\/\/t.co\/f3Nr8ZjYPV\n#PTBR #game #indie #gameplay #VideoGame #VisualNovel #Anime #Encontros #Romance #Casual, #bishoujo #KatawaShoujo #jogosindie #manga #games #gameplay #gameplays #KatawaShoujoemi #emiKatawaShoujo https:\/\/t.co\/iedR1YMRLG","display_text_range":[0,250],"entities":{"hashtags":[{"text":"PTBR","indices":[57,62]},{"text":"game","indices":[63,68]},{"text":"indie","indices":[69,75]},{"text":"gameplay","indices":[76,85]},{"text":"VideoGame","indices":[86,96]},{"text":"VisualNovel","indices":[97,109]},{"text":"Anime","indices":[110,116]},{"text":"Encontros","indices":[117,127]},{"text":"Romance","indices":[128,136]},{"text":"Casual","indices":[137,144]},{"text":"bishoujo","indices":[146,155]},{"text":"KatawaShoujo","indices":[156,169]},{"text":"jogosindie","indices":[170,181]},{"text":"manga","indices":[182,188]},{"text":"games","indices":[189,195]},{"text":"gameplay","indices":[196,205]},{"text":"gameplays","indices":[206,216]},{"text":"KatawaShoujoemi","indices":[217,233]},{"text":"emiKatawaShoujo","indices":[234,250]}],"urls":[{"url":"https:\/\/t.co\/f3Nr8ZjYPV","expanded_url":"https:\/\/youtu.be\/uiG_HaI3ipA","display_url":"youtu.be\/uiG_HaI3ipA","indices":[33,56]}],"user_mentions":[],"symbols":[],"media":[{"id":974376006537699328,"id_str":"974376006537699328","indices":[251,274],"media_url":"http:\/\/pbs.twimg.com\/media\/DYWt0MNX4AAr7H_.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/DYWt0MNX4AAr7H_.jpg","url":"https:\/\/t.co\/iedR1YMRLG","display_url":"pic.twitter.com\/iedR1YMRLG","expanded_url":"https:\/\/twitter.com\/PlayerBanzai\/status\/974376157247393792\/photo\/1","type":"photo","sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":1200,"h":675,"resize":"fit"},"small":{"w":680,"h":383,"resize":"fit"},"large":{"w":1280,"h":720,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":974376006537699328,"id_str":"974376006537699328","indices":[251,274],"media_url":"http:\/\/pbs.twimg.com\/media\/DYWt0MNX4AAr7H_.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/DYWt0MNX4AAr7H_.jpg","url":"https:\/\/t.co\/iedR1YMRLG","display_url":"pic.twitter.com\/iedR1YMRLG","expanded_url":"https:\/\/twitter.com\/PlayerBanzai\/status\/974376157247393792\/photo\/1","type":"photo","sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":1200,"h":675,"resize":"fit"},"small":{"w":680,"h":383,"resize":"fit"},"large":{"w":1280,"h":720,"resize":"fit"}}}]}},"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"PTBR","indices":[57,62]},{"text":"game","indices":[63,68]},{"text":"indie","indices":[69,75]},{"text":"gameplay","indices":[76,85]},{"text":"VideoGame","indices":[86,96]},{"text":"VisualNovel","indices":[97,109]}],"urls":[{"url":"https:\/\/t.co\/f3Nr8ZjYPV","expanded_url":"https:\/\/youtu.be\/uiG_HaI3ipA","display_url":"youtu.be\/uiG_HaI3ipA","indices":[33,56]},{"url":"https:\/\/t.co\/KJaWnStVyB","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/974376157247393792","display_url":"twitter.com\/i\/web\/status\/9\u2026","indices":[111,134]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"pt","timestamp_ms":"1521144353578"}

{"created_at":"Thu Mar 15 20:05:56 +0000 2018","id":974376170111291393,"id_str":"974376170111291393","text":"@OdysseyProtocol The CEO, team, speed of development (When I invested in this in the ICO the market was doing ok an\u2026 https:\/\/t.co\/709gad6CDf","display_text_range":[17,140],"source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e","truncated":true,"in_reply_to_status_id":974349475195039744,"in_reply_to_status_id_str":"974349475195039744","in_reply_to_user_id":941287889916321792,"in_reply_to_user_id_str":"941287889916321792","in_reply_to_screen_name":"CryptonianJake","user":{"id":941287889916321792,"id_str":"941287889916321792","name":"Tsvetan Peshev","screen_name":"CryptonianJake","location":"Vienna, Austria","url":"https:\/\/www.facebook.com\/HungerForTheLife\/?ref=br_rs","description":"Crypto Enthusiast!\n\n#OCN #TRX  #BlackBelt Martial Artist and Champion multiple times.\n\nDisclaimer: Nothing I say Is financial advise, do your own research.","translator_type":"none","protected":false,"verified":false,"followers_count":309,"friends_count":343,"listed_count":2,"favourites_count":2113,"statuses_count":1929,"created_at":"Thu Dec 14 12:44:56 +0000 2017","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"F5F8FA","profile_background_image_url":"","profile_background_image_url_https":"","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\/956861579756220416\/808lK38z_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/956861579756220416\/808lK38z_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/941287889916321792\/1516968220","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"extended_tweet":{"full_text":"@OdysseyProtocol The CEO, team, speed of development (When I invested in this in the ICO the market was doing ok and I got a 4 x 3 days after the ICO ended, turning to a 10x eventually.\nPartnerships are HUGE! #GTC #GAME.COM #TRX #OBIKE and more... #Spring will be #amazing once #crypto is stable.","display_text_range":[17,296],"entities":{"hashtags":[{"text":"GTC","indices":[209,213]},{"text":"GAME","indices":[214,219]},{"text":"TRX","indices":[224,228]},{"text":"OBIKE","indices":[229,235]},{"text":"Spring","indices":[248,255]},{"text":"amazing","indices":[264,272]},{"text":"crypto","indices":[278,285]}],"urls":[],"user_mentions":[{"screen_name":"OdysseyProtocol","name":"The Odyssey Protocol (OCN)","id":946273319036137472,"id_str":"946273319036137472","indices":[0,16]}],"symbols":[]}},"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/709gad6CDf","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/974376170111291393","display_url":"twitter.com\/i\/web\/status\/9\u2026","indices":[117,140]}],"user_mentions":[{"screen_name":"OdysseyProtocol","name":"The Odyssey Protocol (OCN)","id":946273319036137472,"id_str":"946273319036137472","indices":[0,16]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1521144356645"}

它有几个json对象,我如何使用python来读取这个文件并获取信息,例如" hashtags"和" created_at"? json.load总是有错误:期望值:第2行第1列(字符1)

1 个答案:

答案 0 :(得分:0)

该文件似乎包含行分隔的JSON文本,因此您应该能够遍历文件行并将每个文件作为单独的JSON对象加载(在本例中为Python dict),然后通过键访问特定值: / p>

import json

with open('file.json') as f:
    for line in f:
        if line.rstrip():
            data = json.loads(line)
            print(data['created_at'], data['hashtags'])

请注意,"hashtags"不是所提供的JSON数据中的有效密钥,但上述内容应足以解释您的问题。

line.rstrip()用于从行中删除任何尾随空格,以便过滤掉空行。