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
LookupErrorin case the encoding cannot be found.
- codecs.getincrementalencoder(encoding)¶
Look up the codec for the given encoding and return its incremental encoder class or factory function.
Raises a
LookupErrorin case the encoding cannot be found or the codec doesn't support an incremental encoder.
- codecs.getincrementaldecoder(encoding)¶
Look up the codec for the given encoding and return its incremental decoder class or factory function.
Raises a
LookupErrorin case the encoding cannot be found or the codec doesn't support an incremental decoder.
- codecs.getreader(encoding)¶
Look up the codec for the given encoding and return its
StreamReaderclass or factory function.Raises a
LookupErrorin case the encoding cannot be found.
- codecs.getwriter(encoding)¶
Look up the codec for the given encoding and return its
StreamWriterclass or factory function.Raises a
LookupErrorin case the encoding cannot be found.
Custom codecs are made available by registering a suitable codec search function:
- codecs.register(search_function, /)¶
Register a codec search function. Search functions are expected to take one argument, being the encoding name in all lower case letters with hyphens and spaces converted to underscores, and return a
CodecInfoobject. In case a search function cannot find a given encoding, it should returnNone.在 3.9 版的變更: Hyphens and spaces are converted to underscore.
- codecs.unregister(search_function, /)¶
Unregister a codec search function and clear the registry's cache. If the search function is not registered, do nothing.
在 3.10 版被加入.
While the builtin open() and the associated io module are the
recommended approach for working with encoded text files, this module
provides additional utility functions and classes that allow the use of a
wider range of codecs when working with binary files:
- codecs.open(filename, mode='r', encoding=None, errors='strict', buffering=-1)¶
Open an encoded file using the given mode and return an instance of
StreamReaderWriter, providing transparent encoding/decoding. The default file mode is'r', meaning to open the file in read mode.備註
If encoding is not
None, then the underlying encoded files are always opened in binary mode. No automatic conversion of'\n'is done on reading and writing. The mode argument may be any binary mode acceptable to the built-inopen()function; the'b'is automatically added.encoding specifies the encoding which is to be used for the file. Any encoding that encodes to and decodes from bytes is allowed, and the data types supported by the file methods depend on the codec used.
errors may be given to define the error handling. It defaults to
'strict'which causes aValueErrorto be raised in case an encoding error occurs.buffering has the same meaning as for the built-in
open()function. It defaults to -1 which means that the default buffer size will be used.在 3.11 版的變更: 已移除
'U'模式。在 3.14 版之後被棄用:
codecs.open()已被open()取代。
- codecs.EncodedFile(file,