I have a word in russian: "привет".
It is encoded into utf-8 bytes using
'привет'.encode('utf-8')
the result is python bytes object represented as:
b'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'
Now I saved it inside a file and when I read that file I get this string:
"b'\\xd0\\xbf\\xd1\\x80\\xd0\\xb8\\xd0\\xb2\\xd0\\xb5\\xd1\\x82'"
How do I decode this string into the original word?
It is not the bytes object I'm trying to decode but a string, so
"b'\\xd0\\xbf\\xd1\\x80\\xd0\\xb8\\xd0\\xb2\\xd0\\xb5\\xd1\\x82'".decode('utf-8')
returns AttributeError: 'str' object has no attribute 'decode'
The way I save it to a file is simply by calling logger.info(x.encode('utf-8')) which is
import logging
logger = logging.getLogger('GENERATOR_DYNAMICS')
and the way I read a file is
with open('file.log') as f:
logs = f.readlines()
python decode byte string utf8was the dupe - you did not really search a lot, did you? Please read How to Ask - first line of duty is doing research. (not my dv btw)import ast+print("b'\\xd0\\xbf\\xd1\\x80\\xd0\\xb8\\xd0\\xb2\\xd0\\xb5\\xd1\\x82'", ast.literal_eval("b'\\xd0\\xbf\\xd1\\x80\\xd0\\xb8\\xd0\\xb2\\xd0\\xb5\\xd1\\x82'" ).decode("utf8"))