Read Python json. UnicodeDecodeError: 'cp949' codec can't decode byte

Asked 1 years ago, Updated 1 years ago, 101 views

// Enter your code here[
    {
        "created_time": "2018-01-23 08:00:00",
        "link": "http://news.jtbc.joins.com/article/article.aspx?news_id=NB11579263",
        "message": "To former lawmaker Lee Sang-deuk, who was a big shot…\nAre you trying to escape the responsibility of the National Intelligence Service?"
        "name": "[Exclusive] Hundreds of millions of won in special expenses given during the NIS crisis of 'Inni Invasion',
        "num_angrys": 104,
        "num_comments": 12,
        "num_hahas": 13,
        "num_likes": 240,
        "num_loves": 5,
        "num_reactions": 0,
        "num_sads": 4,
        "num_shares": 31,
        "num_wows": 0,
        "post_id": "240263402699918_1665440173515560"
    },
    {
        "created_time": "2018-01-23 07:00:01",
        "link": "http://news.jtbc.joins.com/html/258/NB11579258.html",
        "message": "This is the statement of former secretary Ahn Bong-geun.",
        "name": Choi Soon-sil's attendance at the "\" Park Geun Hye report…Nobody's telling me to get out of here\",
        "num_angrys": 304,
        "num_comments": 51,
        "num_hahas": 119,
        "num_likes": 549,
        "num_loves": 4,
        "num_reactions": 0,
        "num_sads": 1,
        "num_shares": 77,
        "num_wows": 0,
        "post_id": "240263402699918_1665433430182901"
    },
    {
        "created_time": "2018-01-23 06:00:01",
        "link": "https://www.facebook.com/jtbcnews/videos/1665462230180021/",
        "message": "Judges' trial style, family history...\nThe circumstances of the 'background investigation' revealed in the court administration document.",
        "name": "\"Outsider\" \"Student athletic experience\"…The circumstances of the judge's background investigation,"
        "num_angrys": 142,
        "num_comments": 19,
        "num_hahas": 10,
        "num_likes": 163,
        "num_loves": 0,
        "num_reactions": 0,
        "num_sads": 4,
        "num_shares": 57,
        "num_wows": 0,
        "post_id": "240263402699918_1665462230180021"
    }

]

There's a json file like this. I want to get the sum of all num_shares in Python. Reading json by searching

>Traceback (most recent call last):
  File "C:/pyto/jsin.py", line 5, in <module>
    data = json.load(f)
  File "C:\Users\RND1\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 296, in load
    return loads(fp.read(),
UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 165: illegal multibyte sequence

It is similar to the following error How can I read it?

python json unicodedecodeerror

2022-09-22 19:54

1 Answers

The json text you put on seems to work properly.

According to the error code, it seems to be an encoding problem of euc-kr, but if you receive an API response from somewhere and process it, I think you need encoding conversion.


2022-09-22 19:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.