Encoding In Python
'''
Python string can be encode with two encodings 'utf-8' and 'ascii'
'''
string1 = 's̤ Dr hellh̤'
# If it is code with only encode command it will encode with utf-8
print(string1.encode())
#Encode with the error parameters.
print('The encoded version with "ignore" is ', string1.encode('ascii','ignore'))
print(" ")
print('The encoded version with "replace" is ', string1.encode('ascii','replace'))
# encoding - the encoding type a string has to be encoded to
# errors - response when encoding fails. There are six types of error response
# strict - default response which raises a UnicodeDecodeError exception on failure
# ignore - ignores the unencodable unicode from the result
# replace - replaces the unencodable unicode to a question mark ?
# xmlcharrefreplace - inserts XML character reference instead of unencodable unicode
# backslashreplace - inserts a \uNNNN escape sequence instead of unencodable unicode
# namereplace - inserts a \N{...} escape sequence instead of unencodable unicode
Comments
Post a Comment