facebook消息的未知编码

2024-06-16 13:20:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经从facebook请求并下载了我所有的messenger数据,我想解析返回的json进行一些语言分析。在

我的问题是,因为我是法国人,我的大部分谈话都是用法语进行的,而且有很多特殊字符(笑脸也一样):

{
      "sender_name": "Antoine",
      "timestamp_ms": 1493930091160,
      "content": "Comment il est \u00c3\u00a9go\u00c3\u00afste :s",
      "type": "Generic"
    },

下面是一个例子:在messenger中,它拼写为:

"Comment il est égoïste :s"

但是如果我用unicode或utf-8解码unicode字符,我得到的只有:

"Comment il est égoïste"

当我试图将它们写入控制台时,它会崩溃,并显示UnicodeEncodeError。在

到目前为止,我尝试了很多(坏的)regex并替换:

^{pr2}$

但是如果我能找到正确的编码,它会变得更简单、更快(更漂亮),smilies也有一个问题,但我的regex似乎无法捕捉到一些(尤其是当它们被链接起来时)。在

编辑:它可能是以下内容的副本:Facebook JSON badly encoded

而不是提议的那个:)


Tags: 数据语言jsongofacebookunicodecommentmessenger
1条回答
网友
1楼 · 发布于 2024-06-16 13:20:01

我将使用包ftfy来解决这个问题https://github.com/LuminosoInsight/python-ftfy

>>> from ftfy import fix_text
>>> fix_text(u'Comment il est \u00c3\u00a9go\u00c3\u00afste :s')
'Comment il est égoïste :s'

我在安装当前版本时遇到了一些问题,但是它在pip install 'ftfy<5'上运行得很好

相关问题 更多 >