Python codecs Library
The purpose of this module is Encoding and decoding i.e. conversion of the texts between different representations.
This module defines base classes for standard Python codecs (encoders and decoders) and provides access to the internal Python codec registry, which manages the codec and error handling lookup process. Most standard codecs are text encodings, which encode text to bytes (and decode bytes to text), but there are also codecs provided that encode text to text, and bytes to bytes. Custom codecs may encode and decode between arbitrary types, but some module features are restricted to be used specifically with text encodings or with codecs that encode to bytes.
The functions for encoding and decoding with any codec:
1. codecs.encode(obj, encoding='utf-8', errors='strict'):
This function encodes the 'obj' using the codec registered for encoding. With the help of this function we can encode a string into the mentioned encoded form. This returns the encoded string.
By default it's decoding scheme is 'utf-8'. and if we talk about the errors, errors may be given as the argument to set the desired error handling scheme. The default error handler is 'strict' means that the encoding errors raise the ValueError.
EXAMPLE:
If I give the second parameter i.e. encodings as 'hex' then:
EXAMPLE:
2. codecs.decode(obj, encoding='utf-8', errors='strict'):
This function decodes the 'obj' using the codec registered for encoding. With the help of this function we can decode the string into the mentioned decoded form. This returns the decoded string.
By default it's decoding scheme is 'utf-8'. and if we talk about the errors, errors may be given as the argument to set the desired error handling scheme. The default error handler is 'strict' means that the encoding errors raise the ValueError.
EXAMPLE:
If I give the second parameter i.e. encodings as 'hex' then:
EXAMPLE:
3. codecs.lookup(encoding):
This function looks up the codec information in the Python codec registry and then returns a CodecInfo object.
The encodings passed as the parameter is first looked up in the registry's cache. If it is not found, then the list of the registered search functions is scanned. If the CodecInfo is found then the object is stored in the cache and returned to the caller. If no CodecInfo object is found then the function throws a LookupError.
EXAMPLE:
Comments