编码信使JSON [复制]

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

这个问题已经在这里有一个答案:

我正在学习在蟒蛇在JSON我下载分析Facebook的消息使一个简单的程序使用JSON的工作,但这些消息含有大量的被写在JSON文件这样的Unicode字符

POM \ u00c3 \ u00b4 \ u00c5 \ u00bee

上面的例子中被认为是字

帮助

然而,当我尝试用字符串工作,并打印出来的字就出现这样的

'pomôže'

即使大多数在线转换器打印出来这样的,除了这一个https://github.com/mathiasbynens/utf8.js有什么办法解决这一问题?

编辑:好了,所以我是不是足够清晰的遗憾。我们希望,这将让事情更清晰:我有一个JSON文件看起来像这样,在记事本中打开++:

{
    "participants": [
        {
            "name": "Person1"
        },
        {
            "name": "Person2"
        }
    ],
    "messages": [
        {
          "sender_name": "Person1",
          "timestamp_ms": 1521492166805,
          "content": "D\u00c3\u00bafam, \u00c5\u00bee pom\u00c3\u00b4\u00c5\u00bee",
          "type": "Generic"
        }
    ]
}

当我尝试打印或与该消息的内容的工作:

import json
with open("messages.json", "r") as f:
    messages = json.load(f)
    print(messages["messages"][0]["content"])

字符串看起来是这样的:

DAºfam,A¾epomA'A¾e

如何获得文成可读的形式?

python json encoding utf
1个回答
0
投票

我花了一段时间来理解,但它是很容易的原因,字符表被读取在许多方面,你的情况的问题是,要在UTF8但该表UTF8是关系到系统的语言进行打印,你在UTF-16打印

我给你一些例子:

在javascript:

console.log("pom\u{00f4}\u{017E}e");

在Python 3

print("pom"+u"\u00F4"+u"\u017E"+"e")

在Python 2

print("pom"+u"\u00F4".encode('utf-8')+u"\u017E".encode('utf-8')+"e")

doc python 2.X

doc python 3.X

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