Python official documents using notes

--- the codecs and codec register associated base class

  This module defines the base class for standard Python codecs (coder and decoder), and provides an interface registry Python codec used to access the interior of the search process is responsible for managing the registry codec and error handling. Most standard codecs belongs to a text encoding, they can be encoded as a text string of bytes, but also provides some codecs text string of bytes can be encoded as a text string of bytes and encoded. Custom codec may encode and decode between any type, but the characteristics of some of the modules only to text or data encoded as a byte string encoded codec.

  This module provides the following constants for read and write platform-dependent file:

  codecs.BOM_UTF8

  These constants define the various sequence of bytes, i.e., some of the encoding format Unicode byte order mark (BOM). They are used to specify the byte order of the UTF-16 and UTF-32 data stream types, and is used as a signature in Unicode UTF-8 species. BOM_UTF16 is BOM_UTF16_BE or BOM_UTF16_LE, depending on the platform native byte order, BOM is the alias BOM_UTF16, BOM_LE is BOM_UTF16_LE alias, BOM_BE is BOM_UTF16_BE alias. UTF_8 said other sequences and UTF-32 encoding format BOM.

 

  Application examples:

  During compilation error \ 65279

  This is because the code used have bom utf-8 encoding format. bom in fact, at the beginning of the file, insert the bom head of 3bit. '\ Xef \ xbb \ xbf'

import os
import codecs
import configparser

proDir =os.path.split(os.path.realpath(__file__))[0]
configPath = os.path.join(porDir, "config.ini")

class ReadConfig:
    def __init__(self):
        fd = open(configPath)
        data = fd.read()

        # remove BOM
        if data[:3] == codecs.BOM_UTF8:
            data = data[3:]
            file = codes.open(configPaht,"w")
            file.write(data)
            file.close()
        fd.close()

 

Guess you like

Origin www.cnblogs.com/susanhonly/p/11590435.html