python无法读取编码='utf8'的json文件

问题描述 投票:0回答:1

我无法使用此 json 文件阅读全文:

{
  "messages": [
    {
      "sender_name": "test",
      "timestamp_ms": 1554347140802,
      "content": "Ch\u00c3\u00a0o Anh/Ch\u00e1\u00bb\u008b, Anh/Ch\u00e1\u00bb\u008b vui l\u00c3\u00b2ng \u00c4\u0091\u00e1\u00bb\u0083 l\u00e1\u00ba\u00a1i S\u00e1\u00bb\u0090 \u00c4\u0090I\u00e1\u00bb\u0086N THO\u00e1\u00ba\u00a0I + T\u00c3\u008cNH TR\u00e1\u00ba\u00a0NG B\u00e1\u00bb\u0086NH \u00c4\u0091\u00e1\u00bb\u0083 D\u00c6\u00af\u00e1\u00bb\u00a2C S\u00c4\u00a8 CHUY\u00c3\u008aN M\u00c3\u0094N s\u00e1\u00ba\u00afp x\u00e1\u00ba\u00bfp t\u00c6\u00b0 v\u00e1\u00ba\u00a5n v\u00e1\u00bb\u0081 s\u00e1\u00ba\u00a3n ph\u00e1\u00ba\u00a9m, b\u00e1\u00bb\u0087nh t\u00c3\u00acnh c\u00e1\u00bb\u00a5 th\u00e1\u00bb\u0083 v\u00c3\u00a0 li\u00e1\u00bb\u0087u tr\u00c3\u00acnh ph\u00c3\u00b9 h\u00e1\u00bb\u00a3p cho Anh/Ch\u00e1\u00bb\u008b nh\u00c3\u00a9.",
      "is_geoblocked_for_viewer": false
    },
    {
      "sender_name": "",
      "timestamp_ms": 1554334611125,
      "content": "T\u00c3\u00b4i mu\u00e1\u00bb\u0091n \u00c4\u0091\u00e1\u00ba\u00b7t h\u00c3\u00a0ng",
      "is_geoblocked_for_viewer": false
    },
    {
      "sender_name": "test",
      "timestamp_ms": 1554334610788,
      "content": "Ch\u00c3\u00a0o Musickhc! Ch\u00c3\u00bang t\u00c3\u00b4i c\u00c3\u00b3 th\u00e1\u00bb\u0083 gi\u00c3\u00bap g\u00c3\u00ac cho b\u00e1\u00ba\u00a1n?",
      "is_geoblocked_for_viewer": false
    },
    {
      "sender_name": "test",
      "timestamp_ms": 1554334609955,
      "content": "Customer \u00c4\u0091\u00c3\u00a3 tr\u00e1\u00ba\u00a3 l\u00e1\u00bb\u009di tin nh\u00e1\u00ba\u00afn ch\u00c3\u00a0o m\u00e1\u00bb\u00abng t\u00e1\u00bb\u00b1 \u00c4\u0091\u00e1\u00bb\u0099ng c\u00e1\u00bb\u00a7a b\u00e1\u00ba\u00a1n. \u00c4\u0090\u00e1\u00bb\u0083 thay \u00c4\u0091\u00e1\u00bb\u0095i ho\u00e1\u00ba\u00b7c g\u00e1\u00bb\u00a1 l\u00e1\u00bb\u009di ch\u00c3\u00a0o n\u00c3\u00a0y, h\u00c3\u00a3y truy c\u00e1\u00ba\u00adp ph\u00e1\u00ba\u00a7n C\u00c3\u00a0i \u00c4\u0091\u00e1\u00ba\u00b7t tin nh\u00e1\u00ba\u00afn.",
      "is_geoblocked_for_viewer": false
    }
  ]
}

我正在使用此代码:

with open('message_1.json', 'r', encoding='utf-8') as file:
    data = json.loads(file.read())
    print('message', data)
    file.close()

结果是

{'messages': [{'sender_name': 'test', 'timestamp_ms': 1554347140802, 'content': 'ChÃ\xa0o Anh/Chá»\x8b, Anh/Chá»\x8b vui lòng Ä\x91á»\x83 lại Sá»\x90 Ä\x90Iá»\x86N THOáº\xa0I + TÃ\x8cNH TRáº\xa0NG Bá»\x86NH Ä\x91á»\x83 DƯỢC SĨ CHUYÃ\x8aN MÃ\x94N sắp xếp tÆ° vấn vá»\x81 sản phẩm, bá»\x87nh tình cụ thá»\x83 vÃ\xa0 liá»\x87u trình phù hợp cho Anh/Chá»\x8b nhé.', 'is_geoblocked_for_viewer': False}, {'sender_name': '', 'timestamp_ms': 1554334611125, 'content': 'Tôi muá»\x91n Ä\x91ặt hÃ\xa0ng', 'is_geoblocked_for_viewer': False}, {'sender_name': 'test', 'timestamp_ms': 1554334610788, 'content': 'ChÃ\xa0o Musickhc! Chúng tôi có thá»\x83 giúp gì cho bạn?', 'is_geoblocked_for_viewer': False}, {'sender_name': 'test', 'timestamp_ms': 1554334609955, 'content': 'Customer Ä\x91ã trả lá»\x9di tin nhắn chÃ\xa0o mừng tá»± Ä\x91á»\x99ng của bạn. Ä\x90á»\x83 thay Ä\x91á»\x95i hoặc gỡ lá»\x9di chÃ\xa0o nÃ\xa0y, hãy truy cáº\xadp phần CÃ\xa0i Ä\x91ặt tin nhắn.', 'is_geoblocked_for_viewer': False}]}

有人可以帮助我如何使用 utf-8 读取此文件吗? 谢谢

python json utf-8
1个回答
0
投票

我刚刚做到了:

from operator import itemgetter


with open('message_1.json', 'r', encoding='raw_unicode_escape') as file:
    messages = json.loads(file.read().encode('raw_unicode_escape').decode())
    print(messages)
    file.close()

但是不知道有没有更好的办法

© www.soinside.com 2019 - 2024. All rights reserved.