在 Python 3.4 中加载和读取具有多个 JSON 对象的 JSON 文件
Posted
技术标签:
【中文标题】在 Python 3.4 中加载和读取具有多个 JSON 对象的 JSON 文件【英文标题】:Loading and reading a JSON file with multiple JSON objects in Python 3.4 【发布时间】:2018-01-14 15:48:48 【问题描述】:我是 python 的初学者,我需要一些帮助才能读取包含一些推文作为数据的 json 文件。
我已将这些数据保存在 json 文件中,当我尝试读取该文件时。我遇到了一些错误:
ValueError: Expecting value: line 2 column 1 (char 1)
有人可以帮帮我吗?
我的 json 文件内容是: "created_at":"Mon Aug 07 08:38:17 +0000 2017","id":894477786047553536,"id_str":"894477786047553536","text":"@KevinRobMartin @PSTeamOnline @PokerStars Modern 5-7-9扑克牌。Holadeck:\nAHD 规则:All Hands on Deck - all\u2026 https://t.co/gOzpJgwcgX","display_text_range":[42,140],"source":"\u003ca href=\"http: //twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c/a\u003e","truncated":true,"in_reply_to_status_id":894310275763113985,"in_reply_to_status_id_str":"894310275763113985","in_reply_to_user" 2483027940,"in_reply_to_user_id_str":"2483027940","in_reply_to_screen_name":"KevinRobMartin","user":"id":812269114551136256,"id_str":"812269114551136256","name":"Chesmayne","screen" Chesmayne2017","location":"Dublin Ireland Europe Gaia","url":"http://homepage.eircom.net/~reidr1/index.html","description":"Leanne Jordan。游戏网页图形印刷设计师首席执行官和社交媒体:国际象棋、扑克、天文学。通过 G-mail 联系我:chesmayne2014@gmail.com","protected":false,"verifie d":false,"followers_count":251,"friends_count":423,"listed_count":9,"favourites_count":1030,"statuses_count":3597,"created_at":"Fri Dec 23 12:10:22 +0000 2016","utc_offset":null,"time_zone":null,"geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000"," 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":"981CEB","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"000000","profile_use_background_image":false,"profile_image_url":" http://pbs.twimg.com/profile_images/873654899220860929/Gh1zCGk4_normal.jpg","profile_image_url_https":"https://pbs.twimg.com/profile_images/873654899220860929/Gh1zCGk4_normal.jpg","profile_banner_url":"https: //pbs.twimg.com/profile_banners/812269114551136256/1497130468","default_profile" :false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null,"geo":null,"coordinates":null,"place":"id":" 7dde0febc9ef245b","url":"https://api.twitter.com/1.1/geo/id/7dde0febc9ef245b.json","place_type":"city","name":"都柏林市","full_name": "爱尔兰都柏林市","country_code":"IE","country":"Ireland","bounding_box":"type":"Polygon","coordinates":[[[-6.387438,53.298745],[ -6.387438,53.411060],[-6.107805,53.411060],[-6.107805,53.298745]]],"attributes":,"contributors":null,"is_quote_status":false,"extended_tweet":"full_text “:”@KevinRobMartin @PSTeamOnline @PokerStars 现代 5-7-9 纸牌扑克。 Holadeck:\nAHD 规则:所有人都在甲板上 - 所有人都允许\n最新:https://t.co/FDxM8RoITY https://t.co/Rx0Kiu3FRN","display_text_range":[42,161],"entities": "hashtags":[],"urls":["url":"https://t.co/FDxM8RoITY","expanded_url":"https://www.facebook.com/ches.mayne.9 ","display_url":"facebook.com/ches.mayne.9","indices":[138,161]],"user_mentions":["screen_name":"KevinRobMartin","name":"Kevin Martin" ,"id":2483027940,"id_str":"2483027940","indices":[0,15],"screen_name":"PSTeamOnline","name":"Team Pro Online","id":245412615 ,"id_str":"245412615","indices":[16,29],"screen_name":"PokerStars","name":"PokerStars","id":19537263,"id_str":"19537263" ,"indices":[30,41]],"symbols":[],"media":["id":894477755517218818,"id_str":"894477755517218818","indices":[162,185],"media_url ":"http://pbs.twimg.com/media/DGnSx40XsAIUwF_.jpg","media_url_https":"https://pbs.twimg.com/media/DGnSx40XsAIUwF_.jpg","url":"https:// /t.co/Rx0Kiu3FRN","display_url":"pic.twitter.com/Rx0Kiu3FRN","expanded_url":"https: //twitter.com/Chesmayne2017/status/894477786047553536/photo/1","type":"photo","sizes":"medium":"w":960,"h":640,"resize" :"fit","thumb":"w":150,"h":150,"resize":"crop","large":"w":960,"h":640,"调整大小":"fit","small":"w":680,"h":453,"resize":"fit","id":894477778439024643,"id_str":"894477778439024643" ,"索引":[162,185],"media_url":"http://pbs.twimg.com/media/DGnSzONWsAMaj2_.jpg","media_url_https":"https://pbs.twimg.com/media/DGnSzONWsAMaj2_. jpg","url":"https://t.co/Rx0Kiu3FRN","display_url":"pic.twitter.com/Rx0Kiu3FRN","expanded_url":"https://twitter.com/Chesmayne2017/status/ 894477786047553536/photo/1","type":"photo","sizes":"medium":"w":960,"h":640,"resize":"fit","thumb": "w":150,"h":150,"resize":"crop","large":"w":960,"h":640,"resize":"fit","small ":"w":680,"h":453,"resize":"fit"],"extended_entities":"media":["id":894477755517218818,"id_str":" 894477755517218818","indices":[162,185],"media_url":"http://pbs.twimg.com/media/DGnSx40XsAIUwF_.jpg","media_url_https":"ht tps://pbs.twimg.com/media/DGnSx40XsAIUwF_.jpg","url":"https://t.co/Rx0Kiu3FRN","display_url":"pic.twitter.com/Rx0Kiu3FRN","expanded_url" :"https://twitter.com/Chesmayne2017/status/894477786047553536/photo/1","type":"photo","sizes":"medium":"w":960,"h":640 ,"resize":"fit","thumb":"w":150,"h":150,"resize":"crop","large":"w":960,"h" :640,"resize":"fit","small":"w":680,"h":453,"resize":"fit","id":894477778439024643,"id_str" :"894477778439024643","indices":[162,185],"media_url":"http://pbs.twimg.com/media/DGnSzONWsAMaj2_.jpg","media_url_https":"https://pbs.twimg.com/媒体/DGnSzONWsAMaj2_.jpg","url":"https://t.co/Rx0Kiu3FRN","display_url":"pic.twitter.com/Rx0Kiu3FRN","expanded_url":"https://twitter.com/ Chesmayne2017/status/894477786047553536/photo/1","type":"photo","sizes":"medium":"w":960,"h":640,"resize":"fit", "拇指":"w":150,"h":150,"resize":"crop","large":"w":960,"h":640,"resize":"fit" ,"small":"w":680,"h":453,"resize":"fit"],"retweet_count":0,"favorite_count":0,"enti关系":"hashtags":[],"urls":["url":"https://t.co/gOzpJgwcgX","expanded_url":"https://twitter.com/i/web/ status/894477786047553536","display_url":"twitter.com/i/web/status/8\u2026","indices":[117,140]],"user_mentions":["screen_name":"KevinRobMartin"," name":"Kevin Martin","id":2483027940,"id_str":"2483027940","indices":[0,15],"screen_name":"PSTeamOnline","name":"Team Pro Online ","id":245412615,"id_str":"245412615","indices":[16,29],"screen_name":"PokerStars","name":"PokerStars","id":19537263, "id_str":"19537263","indices":[30,41]],"symbols":[],"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level": "low","lang":"en","timestamp_ms":"1502095097444"
filename = 'publictweets.json'
with open(filename, 'r') as f:
count_all = Counter()
for line in f:
tweet = json.loads(line)
【问题讨论】:
请分享您尝试过的代码 您需要将文件内容的代码和示例添加到您的问题中 filename = 'publictweets.json' with open(filename, 'r') as f: count_all = Counter() for line in f: tweet = json.loads(line) 使用all_tweets = json.load(f)
我已经这样做了,但它不起作用。我给我这个错误:ValueError: Extra data: line 3 column 1 - line 3869 column 1 (char 6475 - 708232)
【参考方案1】:
我已经能够解决问题并感谢您的帮助。
filename = 'publictweets.json'
with open(filename, 'r') as f:
for line in f:
if line.strip():
tweet = json.loads(line)
【讨论】:
以上是关于在 Python 3.4 中加载和读取具有多个 JSON 对象的 JSON 文件的主要内容,如果未能解决你的问题,请参考以下文章