Code-encoded format conversion issues in Python

Source: Internet
Author: User
Just came to this company, familiar with the environment, the eldest brother began to let me do a migration, modify the code of work, I want to say is that this kind of work is very boring ~ ~, look at others code, change someone else's code, here to change a variable, where the name of the file, is not technical content, very cumbersome things, But by migrating the code and getting familiar with the environment. Pull so much, talk about today's topic-Code encoding format change, for some reason, need to transfer code from a room to B room, these two can not access each other, but the historical reasons cause a room code is all UTF8 encoding, B room requirements is GBK code, see how this solution.

Coding issues

First talk about why there is a coding problem, take the above example, B computer room This database is all GBK encoded, so the data from the database is GBK, the data from the database is GBK encoded, in the display of the time not garbled, in the case of the data is not taken out of the database conversion, You need to send the header when the code is set to GBK, the output of the file (HTML, TPL, etc.) must be GBK, see the following diagram will be more clear:

DB (GBK) + PHP, etc. (encoding format is unlimited but if the code file has Chinese characters, the file will be GBK encoded or converted to GBK when the Chinese character output) = header (GBK) + HTML, TPL (GBK)

Or there's a way to convert UTF8 to GBK in your code only when you're out of the library, generally utf8 or more popular, and less problematic.

DB (GBK) + PHP (UTF8, and convert data taken from the database to UTF8) + header (UTF8) + HTML, TPL (UTF8)

As long as the above two specifications encoded format, there will be no garbled situation, at least I test the first way is no problem, so I guess the second kind is OK, OK, now write a small script to convert the file encoding format:

#!/usr/bin/python#-*-coding:utf-8-*-#Filename: Changeencode.pyimport osimport sysdef changeencode (File,fromEncode, Toencode): Try:f=open (file) s=f.read () F.close () U=s.decode (Fromencode) S=u.encode (toencode) F=open (fi    Le, "w");  F.write (s) return 0;       Except: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 (Changeencode (_file,fromencode,toencode)!=0): print "[conversion failed:]" +_file Else:print "[Success:]" +_filedef Checkparam (dirname,fromencode,toencode): encode=["UTF-8", "GBK", "GBK", "Utf-8"] if (n OT fromencode in encode or not toencode in encode): Return 2 if (Fromencode==toencode): Return 3 if (not OS.PATH.ISD IR (dirname)): Return 1 return 0if __name__== "__main__": Error={1: "The first parameter is not a valid folder", 3: "Source encoding and target encoding Same", 2: "The encoding you want to convert is no longer within range: U TF-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) 

The script is simple and easy to use

Copy the Code code as follows:


./changeencode.py Target_dir Fromencode Toencode

Here are a few common coding relationships to note:

US-ASCII encoding is a subset of the UTF-8 encoding, this is obtained from the StackOverflow, the original is the following ASCII is a subset of UTF-8, so all the ASCII files are already UTF-8 encoded ,

I tried to do yes, when the Chinese characters are not added to the code US-ASCII, after adding the Chinese characters, to become utf-8.

There is the ASNI encoding format, which represents the local encoding format, for example, in the simplified Chinese operating system, the ASNI code represents GBK encoding, this also need to pay attention to

One more thing is that a command to view the file encoding format under Linux is:

File-i *

You can see the encoding format of the file.

Of course, some of the above may have special characters in the file, the processing time will fail, but the general program file is not a problem.

The above mentioned is the whole content of this article, I hope you can like.

  • 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.