![]() |
|
[Tut] Python decode() - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python decode() (/thread-100252.html) |
[Tut] Python decode() - xSicKxBot - 11-18-2022 Python decode() 5/5 – (1 vote) This tutorial explains the Python Code: decode()Code: decode()![]() Encoding and Decoding – What Does It Mean? ![]() Programs must handle various characters in several languages. Application developers often internationalize programs to display messages and error outputs in various languages, be it English, Russian, Japanese, French, or Hebrew. Python’s string type uses the Unicode Standard to represent characters, which lets Python programs work with all possible characters. Unicode aims to list every character used by human languages and gives each character its unique code. The Unicode Consortium specifications regularly update its specifications for new languages and symbols. ![]() A character is the smallest component of the text. For example, ’a, ‘B’, ‘c’, ‘È’ and ‘Í’ are different characters. Characters vary depending on language or context. For example, the character for “Roman Numeral One” is ‘Ⅰ’, separate from the uppercase letter ‘I’. Though they look the same, these are two different characters that have different meanings. The Unicode standard describes how code points represent characters. A code point value is an integer from 0 to 0x10FFFF. [1] What are Encodings? ![]() A sequence of code points forms a Unicode String represented in memory as a set of code units. These code units are mapped to 8-bit bytes. Character Encoding is the set of rules to translate a Unicode string to a byte sequence. UTF-8 is the most commonly used encoding, and Python defaults to it. UTF stands for “Unicode Transformation Format”, and the ‘8’ refers to 8-bit values used in the encoding. [2] Python decode() ![]() Encoders and decoders convert text between different representations, and specifically, the Python Code: bytesCode: decode()The Code: decode()Code: encode()Code: decode()Here’s the syntax of the method: Code: decode(encoding, error)
str.decode([encoding[, errors]]) # Example:
str.decode(encoding='UTF-8',errors='strict'The Code: decode()Argument | Description Code: encodingSpecifies the encoding to decode. Standard Encodings has a list of all encodings. Code: errorsDecides how to handle the errors: Code: 'strict'Other possible values are: Code: 'ignore'Code: 'replace'Code: 'xmlcharrefreplace'Code: 'backslashreplace'Code: \uNNNN'namereplace' – Inserts a Code: \N{...}Code: codecs.register_error()Example 1 Code: text = "Python Decode converts text string from one encoding scheme to the desired one."
encoded_text = text.encode('ubtf8', 'strict')
print("Encoded String: ", encoded_text)
print("Decoded String: ", encoded_text.decode('utf8', 'strict'))
Example 2 Code: >>> b'\x81abc'.decode("utf-8", "strict")
Traceback (most recent call last): File "<pyshell#55>", line 1, in <module> b'\x81abc'.decode("utf-8", "strict")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 0: invalid start byte
>>> b'\x80abc'.decode("utf-8", "backslashreplace") '\\x80abc'
>>> b'\x80abc'.decode("utf-8", "ignore") 'abc'
|