codecs --- 編解碼器註冊表和基底類別¶
原始碼:Lib/codecs.py
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.
這個模組定義了以下函式,用於以任何編解碼器來編碼與解碼:
- codecs.encode(obj, encoding='utf-8', errors='strict')¶
Encodes obj using the codec registered for encoding.
Errors may be given to set the desired error handling scheme. The default error handler is
'strict'meaning that encoding errors raiseValueError(or a more codec specific subclass, such asUnicodeEncodeError). Refer to Codec Base Classes for more information on codec error handling.
- codecs.decode(obj, encoding='utf-8', errors='strict')¶
Decodes obj using the codec registered for encoding.
Errors may be given to set the desired error handling scheme. The default error handler is
'strict'meaning that decoding errors raiseValueError(or a more codec specific subclass, such asUnicodeDecodeError). Refer to Codec Base Classes for more information on codec error handling.
- codecs.charmap_build(string)¶
Return a mapping suitable for encoding with a custom single-byte encoding. Given a
strstring of up to 256 characters representing a decoding table, returns either a compact internal mapping objectEncodingMapor adictionarymapping character ordinals to byte values. Raises aTypeErroron invalid input.
The full details for each codec can also be looked up directly:
- codecs.lookup(encoding, /)¶
Looks up the codec info in the Python codec registry and returns a
CodecInfoobject as defined below.Encodings are first looked up in the registry's cache. If not found, the list of registered search functions is scanned. If no
CodecInfoobject is found, aLookupErroris raised. Otherwise, theCodecInfoobject is stored in the cache and returned to the caller.
- class codecs.CodecInfo(encode, decode, streamreader=None, streamwriter=None, incrementalencoder=None, incrementaldecoder=None, name=None)¶
Codec details when looking up the codec registry. The constructor arguments are stored in attributes of the same name:
- name¶
編碼的名稱。
- encode¶
- decode¶
The stateless encoding and decoding functions. These must be functions or methods which have the same interface as the
encode()anddecode()methods of Codec instances (see Codec Interface). The functions or methods are expected to work in a stateless mode.
- incrementalencoder¶
- incrementaldecoder¶
Incremental encoder and decoder classes or factory functions. These have to provide the interface defined by the base classes
IncrementalEncoderandIncrementalDecoder, respectively. Incremental codecs can maintain state.
- streamwriter¶
- streamreader¶
Stream writer and reader classes or factory functions. These have to provide the interface defined by the base classes
StreamWriterandStreamReader, respectively. Stream codecs can maintain state.
To simplify access to the various codec components, the module provides
these additional functions which use lookup() for the codec lookup:
- codecs.getencoder(encoding)¶
Look up the codec for the given encoding and return its encoder function.
Raises a
LookupErrorin case the encoding cannot be found.
- codecs.getdecoder(encoding)¶
Look up the codec for the given encoding and return its decoder function.
Raises a