The Code Below Can Encode A String To Utf-8 :
#!/usr/bin/python
# -*- coding: utf-8 -*-
str = 'ورود'
print(str.encode('utf-8'))
That Prints:
b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
But I can't Decode This String With This Code :
#!/usr/bin/python
# -*- coding: utf-8 -*-
str = b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
print(str.decode('utf-8'))
The error is:
Traceback (most recent call last):
File "C:\test.py", line 5, in <module>
print(str.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'
Please Help Me ...
Edit
From the answers switched to a byte string:
#!/usr/bin/python
# -*- coding: utf-8 -*-
str = b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
print(str.decode('utf-8'))
Now the error is:
Traceback (most recent call last):
File "C:\test.py", line 5, in <module>
print(str.decode('utf-8'))
File "C:\Python34\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to <undefined>
str = b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'; print(str.decode('utf-8'))