목차
Python에서 json.loads를 사용할 때 "ValueError: Extra data" 처리
백엔드 개발 파이썬 튜토리얼 Python에서 json.loads를 사용할 때 'ValueError: Extra Data'가 나타나는 이유는 무엇입니까?

Python에서 json.loads를 사용할 때 'ValueError: Extra Data'가 나타나는 이유는 무엇입니까?

Nov 17, 2024 pm 06:11 PM

Why am I getting a

Python에서 json.loads를 사용할 때 "ValueError: Extra data" 처리

문제 설명

데이터를 읽을 때 json.load를 사용하는 JSON 파일에서 "ValueError: Extra data" 오류가 발생할 수 있습니다. 이는 유효한 JSON 데이터 외에 JSON 파일에 추가 후행 콘텐츠가 있음을 의미합니다.

샘플 데이터

다음은 "new" .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))
로그인 후 복사
  1. 별도의 JSON 개체: 이렇게 하면 각 JSON 개체를 목록의 개별 요소로 효과적으로 분리할 수 있습니다.

중간 Python 객체를 피하고 완전한 트윗만 추가되도록 함으로써 이 접근 방식은 메모리 과부하를 방지하고 처리할 수 있게 해줍니다. 대용량 JSON 파일을 효율적으로 처리하세요.

위 내용은 Python에서 json.loads를 사용할 때 'ValueError: Extra Data'가 나타나는 이유는 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

HTML을 구문 분석하기 위해 아름다운 수프를 어떻게 사용합니까? HTML을 구문 분석하기 위해 아름다운 수프를 어떻게 사용합니까? Mar 10, 2025 pm 06:54 PM

HTML을 구문 분석하기 위해 아름다운 수프를 어떻게 사용합니까?

파이썬에서 파일을 다운로드하는 방법 파이썬에서 파일을 다운로드하는 방법 Mar 01, 2025 am 10:03 AM

파이썬에서 파일을 다운로드하는 방법

파이썬의 이미지 필터링 파이썬의 이미지 필터링 Mar 03, 2025 am 09:44 AM

파이썬의 이미지 필터링

Python을 사용하여 텍스트 파일의 ZIPF 배포를 찾는 방법 Python을 사용하여 텍스트 파일의 ZIPF 배포를 찾는 방법 Mar 05, 2025 am 09:58 AM

Python을 사용하여 텍스트 파일의 ZIPF 배포를 찾는 방법

Python을 사용하여 PDF 문서를 사용하는 방법 Python을 사용하여 PDF 문서를 사용하는 방법 Mar 02, 2025 am 09:54 AM

Python을 사용하여 PDF 문서를 사용하는 방법

Django 응용 프로그램에서 Redis를 사용하여 캐시하는 방법 Django 응용 프로그램에서 Redis를 사용하여 캐시하는 방법 Mar 02, 2025 am 10:10 AM

Django 응용 프로그램에서 Redis를 사용하여 캐시하는 방법

Tensorflow 또는 Pytorch로 딥 러닝을 수행하는 방법은 무엇입니까? Tensorflow 또는 Pytorch로 딥 러닝을 수행하는 방법은 무엇입니까? Mar 10, 2025 pm 06:52 PM

Tensorflow 또는 Pytorch로 딥 러닝을 수행하는 방법은 무엇입니까?

NLTK (Natural Language Toolkit) 소개 NLTK (Natural Language Toolkit) 소개 Mar 01, 2025 am 10:05 AM

NLTK (Natural Language Toolkit) 소개

See all articles