Cloth with Python Chinese problem solving method (summed up a number of previous experience, beginners must SEE) _python

Source: Internet
Author: User
Because Python is a self-contained document, you can use the Help function to query the usage explanations for each system function. In general, the key usage and attention points are clearly stated in the documentation for this system. I tried to find the functional interpretation of the Chinese version of the system document on the Internet, but I did not find it, so I decided to learn it by using the English version of the system with its own functional explanations.

If you want to do Tkinter and Wxpython programming, want to know the general widget usage method and attribute introduction, English is not too good, I recommend you, you can see "Python and Tkinter Programming" this book, There are 392 pages to 538 pages of Appendix B and Appendix C selected commonly used functions and almost all the attributes introduced, wonderful not to be missed.

The tool I mentioned above was quickly done. You can query the function without query, and save the keyword key and query results info, so that the next time directly from the list to look up; If you find that you have not checked, then manually add to the list list--is such a simple gadget. Everything seems to be going well. But the problem is also: the English Open, explain the word in some words do not know meaning, after checking the words to write in the info inside, save the next time directly from the hard drive to open look. But in the English info input Chinese, the save process appeared in the decoding problem, that is, decoding to the Chinese part of the following error to eject:

Unicodeencodeerror: ' ASCII ' codec can ' t encode character U ' \u6211 ' in position-not in range (128)

The 61 position is flexible, which is where the info is added to the Chinese language. This error is basically always there, when I want to write the modified info to the file:
Copy Code code as follows:

fp = open (' Tt.txt ', ' W ')
Fp.write (Info.encode ("UTF-8")) # Here's the mistake
Fp.close ()

These three lines do not appear to be wrong in themselves. But there was an error in the middle of this line of code. Is it the wrong way to encode? I have tried many kinds of coding, such as ANSI, UTF-8, Shift_JIS, GB2312, GBK and other coding, found not. So I was confused.

Now I know why it is wrong. The problem is that the modified info is the string variable. The data in info is the string I found from the system through the Help function (that is, the original pure English info) plus a combination of the Chinese I have entered manually. When I was querying system documents from the system, I saved the raw info as follows:
Copy Code code as follows:

fp = open (' Tt.txt ', ' W ')
Fp.write (Info)
Fp.close ()

Note that the wrong is wrong directly to the original info directly written to the file. Do you know what it is when you write the code? You open the Tt.txt, and the way you look at the encoding will know that it is encoded in ANSI format. And that's how the mistake comes about: I query the keyword key, the ANSI format of the string info read into the control display, and then I have manually added the UTF-8 format of the Chinese characters, so connected up the formation of the string info, is a chaotic and has a variety of encoding method of string info, How the system writes cannot write this mixed string info again in Tt.txt using just one encoding.

So, the conclusion is: when you operate in memory, you can arbitrarily regardless of the encoding method, the system will automatically according to the specific situation to judge. But if you want to use Chinese characters, and also to file the way to temporarily save data or strings, please be sure to write the first time the file in utf-8 format, which is the following way:
Copy Code code as follows:

fp = open (' Tt.txt ', ' W ')
Fp.write (Info.encode ("UTF-8"))
Fp.close ()

This will ensure that the next time you read it, you can print and display it directly without converting the encoding, even if it is not a problem as a control text. Be sure to pay attention to this point.

The problem has been found, there are some other discussions below.

Some people say, as long as the use of the #-*-Coding:utf-8-*-not on the line? Fact

Through my tests (I use the idle (Python2.5.4 GUI) compiler. "1" Whether I start with the use of the #-*-Coding:utf-8-*-, or whether the software is set to use the default Utf-8 encoding, Chinese in the control and file between the use is no problem. "2" info= ' Chinese '; Such an operation is possible. Read the time to use the general reading method on it. The reason I think is because the compiler upgrade, solve the problem of Chinese display and use, the early Chinese language can not use the situation now no longer exist.
Copy Code code as follows:

#coding =utf-8
Try
Jap=open ("Jap.txt", "R")
Chn=open ("Chn.txt", "R")
Utf=open ("Utf.txt", "W")

Jap_text=jap.readline ()
Chn_text=chn.readline ()
#先decode成UTF-16, then encode into UTF-8.
Jap_text_utf8=jap_text.decode ("Shift_JIS"). Encode ("UTF-8")
#不转成utf-8 is OK.
Chn_text_utf8=chn_text.decode ("GB2312"). Encode ("UTF-8")
#编码方式大小写都行utf-8, too.
Utf.write (Jap_text_utf8)
Utf.write (Chn_text_utf8)
Utf.close ()
Except Ioerror,e:
Print "Open File Error", E

This is the code I extracted from Http://www.jb51.net/article/26542.htm's "Learning Python coding" article. Here to explain, the above Jap_text_utf8 and Chn_text_utf8 to ensure that the machine is the default encoding, or UTF-8 encoding, the most important thing is to maintain consistency. After a unified coding for utf-8, you can write to a file, read out again to use is no problem. Use the following common way to read:
Copy Code code as follows:

Filen = open (' Tt.txt ')
info = Filen.read ()
Print Info

Other than that. Someone uses the following method to encode and convert:
Copy Code code as follows:

Import Sys
Reload (SYS)
Sys.setdefaultencoding (' UTF8 ')

def CONVERTCN (s):
Return S.encode (' GB18030 ')

def printfile (filename):
f = file (filename, ' R ')
For F_line in F.readlines ():
Print CONVERTCN (f_line)
F.close ()

if __name__ = = "__main__":
Printfile (' 1.txt ')
Print CONVERTCN ("\n****** Press any key to exit!") ******")
Print Sys.stdin.readline ()

Through my tests, this approach is not feasible. If the second line is removed, the setdefaultencoding function in the third row will be invalid, and if the second row is left, the third and subsequent code will not be executed (although not the error). Please try it out if this method is feasible.
   In addition, "Python Chinese garbled problem in-depth analysis" in the article http://www.jb51.net/article/26543.htm a lot of text how to encode the problem, I was eye-opening. The principle of text encoding: The original is to add the appropriate annotation symbol at the beginning of the text to represent the internal encoding method, so the interpreter will use some corresponding rules to the byte of a certain step or flexible way to translate bytes, get the original text, the translation of the step and the rules are completely the beginning of the description of the corresponding. So, if your text is a single byte encoding, then you can add a proper rule to the top of your code, telling others how to translate your encoded text. Bom_utf_8 and other text at the end of the knowledge is also very interesting, similar to bom_utf_16 and so on, different coding methods at the end of the symbols are different, we can pay attention to.

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.