python kann in utf-8 kodieren, aber nicht dekodieren
Der unten stehende Code kann einen String in Utf-8 kodieren:
#!/usr/bin/python
# -*- coding: utf-8 -*-
str = 'ورود'
print(str.encode('utf-8'))
Das druckt:
b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
Aber ich kann diesen String nicht mit diesem Code dekodieren:
#!/usr/bin/python
# -*- coding: utf-8 -*-
str = b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
print(str.decode('utf-8'))
Der Fehler ist:
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'
Bitte hilf mir ..
BearbeiteVon den Antworten wurde auf eine Byte-Zeichenfolge gewechselt:
#!/usr/bin/python
# -*- coding: utf-8 -*-
str = b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
print(str.decode('utf-8'))
Nun ist der Fehler:
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>