4

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.

asked Dec 3, 2018 at 21:50
13
  • 1
    'ø' is '\u00f8' Commented Dec 3, 2018 at 21:53
  • Your string is in fact: 'første student' Commented Dec 3, 2018 at 21:54
  • 2
    @Rafael That will not help # -*- coding: utf-8 -*- is specifing the file encoding of the source code only. Commented Dec 3, 2018 at 21:57
  • 3
    @vhflat: Sorru; I reopened. Commented Dec 3, 2018 at 22:10
  • 1
    Possible duplicate of Facebook JSON badly encoded Commented Feb 24, 2019 at 10:03

1 Answer 1

8

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'
answered Dec 3, 2018 at 22:16
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.