Requirements: Convert Utf-8 formatted files to GBK formatted files
The implementation code is as follows:
The code is as follows:
def ReadFile (filepath,encoding= "Utf-8"):
With Codecs.open (FilePath, "R", encoding) as F:
Return F.read ()
def writefile (filepath,u,encoding= "GBK"):
With Codecs.open (FilePath, "w", Encoding) as F:
F.write (U)
def UTF8_2_GBK (SRC,DST):
Content = ReadFile (src,encoding= "Utf-8")
WriteFile (dst,content,encoding= "GBK")
Code Explanation:
The second parameter of the function ReadFile specifies that the file is read in utf-8 format, the returned content is Unicode, and then the Unicode is written to the file in GBK format.
This will enable the requirements to be fulfilled.
However, if the file to be converted contains some characters that are not included in the GBK character set, it will be an error, similar to the following:
The code is as follows:
Unicodeencodeerror: ' GBK ' codec can ' t encode character U ' \xa0 ' in position 4813:illegal multibyte sequence
The above error message means: When Unicode is encoded into GBK, Unicode U ' \xa0 ' cannot be encoded as GBK.
Here, we need to figure out the relationship between gb2312, GBK and GB18030.
The code is as follows:
gb2312:6763 a Chinese character
Gbk:21003 a Chinese character
gb18030-2000:27533 a Chinese character
gb18030-2005:70244 a Chinese character
So, GBK is a superset of GB2312, and GB18030 is a superset of GBK.
After clearing the relationship, we further refine the code:
The code is as follows:
def UTF8_2_GBK (SRC,DST):
Content = ReadFile (src,encoding= "Utf-8")
WriteFile (dst,content,encoding= "GB18030")
After running, found that no error, can run normally.
Because, in the GB18030 character set, you can find the corresponding character of U ' \xa0 '.
In addition, there is another implementation scenario:
Need to modify the next WriteFile method
The code is as follows:
def writefile (filepath,u,encoding= "GBK"):
With Codecs.open (FilePath, "w") as F:
F.write (U.encode (encoding,errors= "Ignore"))
Here, we use the Unicode encoding (encode) into the GBK format, but note the second argument of the Encode function, we assign the value "ignore", which means that when encoding, ignore those characters that cannot be encoded, decoding the same.
However, when we do, we find that we can successfully modify the UTF-8 format file to ANSI format. However, in addition to discovering the resulting file, each row has a blank line.
Here, you can specify to write the file as a binary stream, and the modified code is as follows:
The code is as follows:
def writefile (filepath,u,encoding= "GBK"):
With Codecs.open (FilePath, "WB") as F:
F.write (U.encode (encoding,errors= "Ignore"))