Search code examples
python-3.xparsingpython-3.5bsoncyrillic

how to decode bson in cyrillic?


i got a bson {'room': '55d5928a4d02f4a55007344c', 'sender': '55af71054d02f4571abe1f1f', 'message': 'Ð\x9fÑ\x80ивеÑ\x82! Ð\x97деÑ\x81Ñ\x8c вÑ\x8b можеÑ\x82е задаÑ\x82Ñ\x8c вопÑ\x80оÑ\x81 и бÑ\x8bÑ\x81Ñ\x82Ñ\x80о полÑ\x83Ñ\x87иÑ\x82Ñ\x8c оÑ\x82веÑ\x82 на него.', 'created_at': 1440146705.612493, '_id': ObjectId('55d6e5114d02f49f998a08b5')} and i want to get 'message' out of it in python3. how do i decode it?

I already tried this raw.encode().decode('utf8') raw.encode().decode('cp1251') but it gives bad result 'Привет! Здесь вы можете задать вопрос и быстро получить ответ на него.'


Solution

  • This is UTF-8 which has been improperly decoded as Latin-1 (ISO 8859-1).

    >>> raw.encode('l1').decode()
    "{'room': '55d5928a4d02f4a55007344c', 'sender': '55af71054d02f4571abe1f1f', 'message': 'Привет! Здесь вы можете задать вопрос и быстро получить ответ на него.', 'created_at': 1440146705.612493, '_id': ObjectId('55d6e5114d02f49f998a08b5')}"