4.2 Codecs--codec Registration Management and base class

Source: Internet
Author: User

This module defines python standard codecs (encoding and decoding) the base class, also provides a pair of codec Span style= "font-family: the song Body;" > the registration, management and data processing process. Most of the standard codecs are all text codecs, It is mainly used to encode how the text is encoded into bytes. However, there are some is used to encode from text to text, or from bytes to bytes. Of course, you can also customize the codecs codecs is limited to text codec, and some is limited to encoding between bytes.

For any codec codec , The following function is available:

Codecs.encode (obj[, encoding[, errors])

Use the already registered Encoder encoding to encode the object obj and, if there is an error, treat it as errors . The default encoder is utf-8. The error handling parameter , errors , can be customized by using a predetermined or later method. The default is strict processing, which throws an exception valueerror when an error occurs .

Example:

#python 3.4.3import Codecsen = Codecs.encode (' Build Shenzhen into the World Software Center ', ' utf_8 ', ' strict ') print (en)

The resulting output is as follows:

B ' \xe6\x8a\x8a\xe6\xb7\xb1\xe5\x9c\xb3\xe5\xbb\xba\xe8\xae\xbe\xe6\x88\x90\xe4\xb8\x96\xe7\x95\x8c\xe8\xbd\xaf \xe4\xbb\xb6\xe4\xb8\xad\xe5\xbf\x83 '

Codecs.decode (obj[, encoding[, errors])

The object obj is decoded using the already registered Encoder encoding , and if there is an error, it is errors handled by the error mechanism . The default encoder is utf-8. The error handling parameter , errors , can be customized by using a predetermined or later method. The default is strict processing, which throws an exception valueerror when an error occurs .

Example:

#python 3.4.3import Codecsen = Codecs.encode (' Build Shenzhen into the World Software Center ', ' utf_8 ', ' strict ') print (en) de = Codecs.decode (en, ' utf_8 ', ' Strict ') print (DE)

The result output is as follows: B ' \xe6\x8a\x8a\xe6\xb7\xb1\xe5\x9c\xb3\xe5\xbb\xba\xe8\xae\xbe\xe6\x88\x90\xe4\xb8\x96\xe7\x95\x8c\xe8\ Xbd\xaf\xe4\xbb\xb6\xe4\xb8\xad\xe5\xbf\x83 ' to build Shenzhen into the World Software Center codecs.lookup (encoding) to find codecs by the name encoding of the codec, If the returned Codecinfo object is found successfully, an exception lookuperror is thrown. Example:

#python 3.4.3import codecscinfo = codecs.lookup (' utf_8 ') print (cinfo) Cinfo = Codecs.lookup (' gb2312 ') print (cinfo) cinfo = Codecs.lookup (' Test ') print (cinfo)

The resulting output is as follows:

<codecs. Codecinfo object for encoding utf-8 at 0x29a6500>

<codecs. Codecinfo object for encoding gb2312 at 0x29bf420>

Traceback (most recent):

File "e:/pywin/codecs1.py", line ten, in <module>

Cinfo = codecs.lookup (' Test ')

Lookuperror:unknown Encoding:test

Codecs.getencoder (encoding)

The encoding function of the encoder is obtained by encoding , simplifying repeated calls to the encoding. If the throw exception lookuperror is not found .

Example:

#python 3.4.3import codecscinfo = Codecs.getencoder (' utf_8 ') print (cinfo) en = Cinfo (' shenzhen Software ') print (en) cinfo = Codecs.getencoder (' gb2312 ') print (cinfo)

The resulting output is as follows:

<built-in function utf_8_encode>

(b ' \xe6\xb7\xb1\xe5\x9c\xb3\xe8\xbd\xaf\xe4\xbb\xb6 ', 4)

<built-in method Encode of Multibytecodec object at 0x029fb150>

Codecs.getdecoder (encoding)

Gets the function of the decoder through encoding. Simplifies repeated calls to decoding. If the throw exception lookuperror is not found .

Example:

#python 3.4.3

Import Codecs

Cinfo = Codecs.getdecoder (' utf_8 ')

Print (Cinfo)

De = Cinfo (b ' \xe6\xb7\xb1\xe5\x9c\xb3\xe8\xbd\xaf\xe4\xbb\xb6 ')

Print (DE)

The resulting output is as follows:

<function Decode at 0x00261e88>

(' shenzhen software ', ')

Codecs.getincrementalencoder (encoding)

Returns an encoding encoded incremental encoder. Throw exception Lookuperror if not found .

Example:

#python 3.4.3

Import Codecs

Encoder = Codecs.getincrementalencoder (' Idna ') ()

Print (encoder)

Encoder.reset ()

En = Encoder.encode (U "\xe4x")

En = Encoder.encode (U "ample.org")

En = Encoder.encode (U "", True)

Print (en)

The resulting output is as follows:

<encodings.idna.incrementalencoder Object at 0x029bb4b0>

B ' org '

Codecs.getincrementaldecoder (encoding)

Returns an encoding encoded Delta decoder. Throw exception Lookuperror if not found .

Example:

#python 3.4.3

Import Codecs

D = Codecs.getincrementaldecoder ("Utf-8-sig") ()

s = u "spam"

Print (D.decode (S.encode ("Utf-8-sig")))

The resulting output is as follows:

Spam

Codecs.getreader (encoding)

Returns an encoding encoded stream that reads an object StreamReaderIf it is not found to throw an exception lookuperror.

Example:

#python 3.4.3

Import Codecs

Import Urllib.request, JSON

f = Urllib.request.urlopen ("Https://pypi.python.org/pypi/{}/{}/json").

Format (' Jsonpatch ', ' 1.11 ' or ')

Reader = Codecs.getreader ("Utf-8")

Pkg_data = Json.load (Reader (f))

F.close ()

D = {}

d[' name '] = pkg_data[' info ' [' name ']

d[' homepage ' = pkg_data[' info '].get (' home_page ', ')

Print (d)

The resulting output is as follows:

{' homepage ': ' Https://github.com/stefankoegl/python-json-patch ', ' name ': ' Jsonpatch '}

Codecs.getwriter (encoding)

Returns a encoding Decoded stream written to the object StreamWriterIf it is not found to throw an exception lookuperror.

Example:

#python 3.4.3

Import io, codecs

s = io. Bytesio ()

c = codecs.getwriter (' GB18030 ') (s)

C.write (' Test ')

Print (S.getvalue ())

The resulting output is as follows:

B ' Test '


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

4.2 Codecs--codec Registration Management and base class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.