How can I decode this string in python?

ⅰ亾dé卋堺 提交于 2021-02-08 23:44:13

问题


I downloaded a dataset of facebook messages and it was formatted like this:

f\u00c3\u00b8rste student

It's supposed to be første student but I cant seem to decode it correctly.

I tried:

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'

But it did't work.


回答1:


To undo whatever encoding foulup has taken place, you first need to convert the characters to the bytes with the same ordinals by encoding in ISO-8859-1 (Latin-1) and then after that decoding as UTF-8:

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


来源:https://stackoverflow.com/questions/53602446/how-can-i-decode-this-string-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!