*Memo:
- My post explains a string (1).
- My post explains a string (2).
- My post explains str() (1).
- My post explains a bytes.
- My post explains a bytearray.
<Decode a byte string to a string>:
print(str(object=b'', encoding='utf-8', errors='strict'))
print(str(encoding='utf-8', errors='strict'))
print(str(encoding='utf-8'))
print(str(errors='strict'))
# Nothing
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='utf-8')
print(b)
# b"L\xd1\x91\xd1\x82's g\xcf\x86!"
print(str(object=b, encoding='utf-8'))
# Lёт's gφ!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='utf-7')
print(b)
# b"L+BFEEQg's g+A8Y!"
print(str(object=b, encoding='utf-7'))
# Lёт's gφ!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='utf-16')
print(b)
# b"\xff\xfeL\x00Q\x04B\x04'\x00s\x00 \x00g\x00\xc6\x03!\x00"
print(str(object=b, encoding='utf-16'))
# Lёт's gφ!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='big5')
print(b)
# b"L\xc7\xce\xc7\xdb's g\xa3p!"
print(str(object=b, encoding='big5'))
# Lёт's gφ! (Let's go!)
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='ignore')
print(b)
# b"L's g!"
print(str(object=b, encoding='ascii', errors='ignore'))
# L's g!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='replace')
print(b)
# b"L??'s g?!"
print(str(object=b, encoding='ascii', errors='replace'))
# L??'s g?!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='xmlcharrefreplace')
print(b)
# b"Lёт's gφ!"
print(str(object=b, encoding='ascii', errors='xmlcharrefreplace'))
# Lёт's gφ!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='backslashreplace')
print(b)
# b"L\\u0451\\u0442's g\\u03c6!"
print(str(object=b, encoding='ascii', errors='backslashreplace'))
# L\u0451\u0442's g\u03c6!
import codecs
def hashreplace_handler(x):
return ((x.end - x.start) * '#', x.end)
codecs.register_error('hashreplace', hashreplace_handler)
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='hashreplace')
print(b)
# b"L##'s g#!"
print(str(object=b, encoding='ascii', errors='hashreplace'))
# L##'s g#!
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='ascii', errors='strict')
# UnicodeEncodeError: 'ascii' codec can't encode characters
# in position 1-2: ordinal not in range(128)
import codecs
def hashreplace_handler(x):
return ((x.end - x.start) * '#', x.end)
codecs.register_error('hashreplace', hashreplace_handler)
v = "Lёт's gφ!" # Let's go!
b = v.encode(encoding='utf-8', errors='strict')
print(b)
# b"L\xd1\x91\xd1\x82's g\xcf\x86!"
print(str(object=b, encoding='ascii', errors='ignore'))
# L's g!
print(str(object=b, encoding='ascii', errors='replace'))
# L����'s g��!
print(str(object=b, encoding='ascii', errors='backslashreplace'))
# L\xd1\x91\xd1\x82's g\xcf\x86!
print(str(object=b, encoding='ascii', errors='hashreplace'))
# L####'s g##!
print(str(object=b, encoding='ascii', errors='strict'))
# UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1
# in position 1: ordinal not in range(128)
print(str(object=b, encoding='ascii', errors='xmlcharrefreplace'))
# TypeError: don't know how to handle UnicodeDecodeError in error callback
Top comments (0)