Python -- code encoding format conversion

Source: Internet
Author: User

Python -- code encoding format conversion
Let's talk about the encoding problem first. In the above example, the database in data center B is all GBK encoded, so the data retrieved from the database is GBK, the data retrieved from the database is encoded in GBK format. It must be displayed without garbled characters. If the data retrieved from the database is not converted, you need to set the encoding to GBK when sending the header. The output files (such as html and tpl) must all be GBK. See the following figure for more information: DB (GBK) => php (the encoding format is not limited, but if the code file contains Chinese characters, the file must be gbk encoded or converted to gbk when the Chinese characters are output) => header (GBK) => html, tpl (GBK), or another method is to convert utf8 to gbk in the Code only when the database is released. In general, utf8 is more popular, more problems: DB (GBK) => php (utf8, and convert the data retrieved from the database to utf8) => header (utf8) => html, tpl (utf8) as long There will be no garbled characters in the above two standard encoding formats. At least the first method I tested is okay, so I guess the second method is OK, now let's write a small script for converting the file encoding format :#! /Usr/bin/python #-*-coding: UTF-8-*-# Filename: changeEncode. pyimport osimport sys def ChangeEncode (file, fromEncode, toEncode): try: f = open (file) s = f. read () f. close () u = s. decode (fromEncode) s = u. encode (toEncode) f = open (file, "w"); f. write (s) return 0; doesn t: return-1; def Do (dirname, fromEncode, toEncode): for root, dirs, files in OS. walk (dirname): for _ file in files: _ file = OS. path. join (root, _ file) if (C HangeEncode (_ file, fromEncode, toEncode )! = 0): print "[Conversion failed:]" + _ file else: print "[success:]" + _ file def CheckParam (dirname, fromEncode, toEncode ): encode = ["UTF-8", "GBK", "gbk", "UTF-8"] if (not fromEncode in encode or not toEncode in encode ): return 2 if (fromEncode = toEncode): return 3 if (not OS. path. isdir (dirname): return 1 return 0 if _ name __= = "_ main _": error = {1: "The first parameter is not a valid folder", 3: "the source and target encoding are the same", 2: "The encoding you want to convert is no longer within the range: UTF-8, GBK "} dirname = sys. argv [1] FromEncode = sys. argv [2] toEncode = sys. argv [3] ret = CheckParam (dirname, fromEncode, toEncode) if (ret! = 0): print error [ret] else: Do (dirname, fromEncode, toEncode) script is very simple and easy to use. /changeEncode. py target_dir fromEncode toEncode note the relationship between several common encodings: us-ascii encoding is a subset of UTF-8 encoding, which is obtained from stackoverflow, the original text is ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded, I tried it is indeed, when not adding Chinese characters show encoding for us-ascii, after adding Chinese characters, it becomes UTF-8. There is also the ASNI encoding format, which represents the local encoding format. For example, in the simplified Chinese operating system, ASNI encoding represents GBK encoding, note that the command for viewing the file encoding format in linux is file-I.

Related Article

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.