Python で json.loads を使用すると「ValueError: Extra Data」が発生するのはなぜですか?
Python で json.loads を使用する場合の「ValueError: Extra data」への対処
問題の説明
データ読み取り時json.load を使用して JSON ファイルからデータを取得すると、「ValueError: Extra data」エラーが発生する場合があります。これは、JSON ファイル内に有効な JSON データ以外の追加の末尾コンテンツがあることを示唆しています。
サンプル データ
以下は、「新しい JSON データ」内のデータのサンプルです。 .json" ファイルが原因で、エラー:
{ "contributors": null, "truncated": false, "text": "@HomeShop18 #DreamJob to professional rafter", "in_reply_to_status_id": null, "id": 421584490452893696, "favorite_count": 0, "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Mobile Web (M2)</a>", "retweeted": false, "coordinates": null, "entities": { "symbols": [], "user_mentions": [ { "id": 183093247, "indices": [ 0, 11 ], "id_str": "183093247", "screen_name": "HomeShop18", "name": "HomeShop18" } ], "hashtags": [ { "indices": [ 12, 21 ], "text": "DreamJob" } ], "urls": [] }, "in_reply_to_screen_name": "HomeShop18", "id_str": "421584490452893696", "retweet_count": 0, "in_reply_to_user_id": 183093247, "favorited": false, "user": { "follow_request_sent": null, "profile_use_background_image": true, "default_profile_image": false, "id": 2254546045, "verified": false, "profile_image_url_https": "https://pbs.twimg.com/profile_images/413952088880594944/rcdr59OY_normal.jpeg", "profile_sidebar_fill_color": "171106", "profile_text_color": "8A7302", "followers_count": 87, "profile_sidebar_border_color": "BCB302", "id_str": "2254546045", "profile_background_color": "0F0A02", "listed_count": 1, "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "utc_offset": null, "statuses_count": 9793, "description": "Rafter. Rafting is what I do. Me aur mera Tablet. Technocrat of Future", "friends_count": 231, "location": "", "profile_link_color": "473623", "profile_image_url": "http://pbs.twimg.com/profile_images/413952088880594944/rcdr59OY_normal.jpeg", "following": null, "geo_enabled": false, "profile_banner_url": "https://pbs.twimg.com/profile_banners/2254546045/1388065343", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "name": "Jayy", "lang": "en", "profile_background_tile": false, "favourites_count": 41, "screen_name": "JzayyPsingh", "notifications": null, "url": null, "created_at": "Fri Dec 20 05:46:00 +0000 2013", "contributors_enabled": false, "time_zone": null, "protected": false, "default_profile": false, "is_translator": false }, "geo": null, "in_reply_to_user_id_str": "183093247", "lang": "en", "created_at": "Fri Jan 10 10:09:09 +0000 2014", "filter_level": "medium", "in_reply_to_status_id_str": null, "place": null }
解決策
このエラーは、json.load がファイル全体を単一の JSON オブジェクトとして解析しようとするために発生します。ただし、ファイルには複数の行が含まれており、それぞれが個別の JSON オブジェクトを表します。
この問題を解決するには、次の手順に従います。
- 行ごとに繰り返します:ファイルを 1 行ずつ反復処理し、次を使用して各行を JSON としてロードします。 json.loads.
tweets = [] with open('new.json') as infile: for line in infile: tweets.append(json.loads(line))
- JSON オブジェクトの分離: これを行うことで、各 JSON オブジェクトをリスト内の個々の要素に効果的に分離できます。
このアプローチでは、中間の Python オブジェクトを回避し、完全なツイートのみが追加されるようにすることで、メモリの過負荷を防ぎ、大きな JSON ファイルを効率的に処理します。
以上がPython で json.loads を使用すると「ValueError: Extra Data」が発生するのはなぜですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











LinuxターミナルでPythonバージョンを表示する際の許可の問題の解決策PythonターミナルでPythonバージョンを表示しようとするとき、Pythonを入力してください...

fiddlereveryversings for the-middleの測定値を使用するときに検出されないようにする方法

PythonのPandasライブラリを使用する場合、異なる構造を持つ2つのデータフレーム間で列全体をコピーする方法は一般的な問題です。 2つのデータがあるとします...

UvicornはどのようにしてHTTPリクエストを継続的に聞きますか? Uvicornは、ASGIに基づく軽量のWebサーバーです。そのコア機能の1つは、HTTPリクエストを聞いて続行することです...

10時間以内にコンピューター初心者プログラミングの基本を教える方法は?コンピューター初心者にプログラミングの知識を教えるのに10時間しかない場合、何を教えることを選びますか...

Investing.comの反クラウリング戦略を理解する多くの人々は、Investing.com(https://cn.investing.com/news/latest-news)からのニュースデータをクロールしようとします。
