【问题标题】:How can I decode this string in python?如何在 python 中解码这个字符串?
【发布时间】:2019-05-05 06:14:42
【问题描述】:

我下载了一个 Facebook 消息数据集,其格式如下:

f\u00c3\u00b8rste student

应该是første student,但我似乎无法正确解码。

我试过了:

str = 'f\u00c3\u00b8rste student'
print(str)
# 'første student'

str = 'f\u00c3\u00b8rste student'
print(str.encode('utf-8')) 
# b'f\xc3\x83\xc2\xb8rste student'

但它没有用。

【问题讨论】:

  • 'ø''\u00f8'
  • 你的字符串实际上是:'første student'
  • @Rafael 这无济于事# -*- coding: utf-8 -*- 仅指定源代码的文件编码。
  • @vhflat:索鲁;我重新打开了。

标签: python unicode utf


【解决方案1】:

要撤消发生的任何编码错误,您首先需要通过 ISO-8859-1 (Latin-1) 编码将字符转换为具有相同序数的字节,然后解码为 UTF-8:

>>> 'f\u00c3\u00b8rste student'.encode('iso-8859-1').decode('utf-8')
'første student'

【讨论】:

    猜你喜欢
    • 2011-01-15
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    相关资源
    最近更新 更多