Python: Convert utf-8 formatted files to GBK formatted files

Source: Internet
Author: User
Tags readfile

Requirement: Convert the Utf-8 format file to the GBK format file implementation code as follows:
defReadFile (filepath,encoding="Utf-8"): With Codecs.open (FilePath,"R", encoding) as F:returnF.read ()defWriteFile (filepath,u,encoding="GBK"): With Codecs.open (FilePath,"W", encoding) as F:f.write (U)defUTF8_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: unicodeencodeerror: ' GBK ' codec can ' t encode character U ' \xa0 ' in position 4813:illegal multibyte sequenceThe 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 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:
def UTF8_2_GBK (SRC,DST):     = 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: The next WriteFile method needs to be modified
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 in the same vein. 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:
def WriteFile (filepath,u,encoding="gbk"):    with Codecs.open (FilePath,"  WB") as F:        F.write (U.encode (encoding,errors="ignore "))
Related article: http://www.crifan.com/python_csv_writer_writerow_redundant_new_line/
http://www.v2ex.com/t/40033

Python: Convert utf-8 formatted files to GBK formatted files

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.