Adaptation and processing of related files in Python

Source: Internet
Author: User

In Python, there are many difficulties in Chinese, that is, the beginner's "Ke Xing". The following article provides related solutions to this problem, we hope we can get some good methods to solve these problems, so that we can better flexibly use them in computer operation.

Import sys. version & apos; 2.5.1 (r5.1: 54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] & apos; use NotePad to create a file ChineseTest. py, default ANSI:
S = "Chinese"
Print s

Test it:

 
 
  1. E: \ Project \ Python \ Test> python Chinese Python ChineseTest. py
  2. File "ChineseTest. py", line 1
  3. SyntaxError: Non-ASCII character & apos; \ xd6 & apos; in file
    ChineseTest. py on line 1, but no encoding declared;

Secretly change the file encoding to UTF-8:

 
 
  1. E: \ Project \ Python \ Test> python Chinese ChineseTest. py
  2. File "ChineseTest. py", line 1
  3. SyntaxError: Non-ASCII character & apos; \ xe4 & apos; in file
    ChineseTest. py on line 1, but no encoding declared; no help.
  4.  

Now that it provides a URL, let's take a look. After a brief look, we finally know that if the file contains non-ASCII characters, We need to specify the encoding declaration in the first or second line. Change the encoding of the ChineseTest. py file to ANSI and add the encoding statement.

 
 
  1. # Coding = gbk
  2. S = "Chinese"
  3. Print s:
  4. E: \ Project \ Python \ Test> python ChineseTest. py
  5. Chinese is normal :)

I)Take a look at its length:

 
 
  1. # Coding = gbk
  2. S = "Chinese"
  3. Print len (s)

S is 'str' type. Therefore, a Chinese character is equivalent to two English characters, so the length is 4.
We write it like this

 
 
  1. # Coding = gbk
  2. S = "Chinese"
  3. S1 = u "Chinese"
  4. S2 = unicode (s, "gbk") # omitting parameters will be decoded using the default ASCII in python Chinese
  5. Ss3 = s. decode ("gbk") # convert str to unicode: decode. unicode Function
    Same role
  6. Print len (s1)
  7. Print len (s2)
  8. Print len (s3)
  9.  

 II) Then let's take a look at the processing of the file.:

Create a file named test.txt in ANSI format with the following content:
Abc Chinese
Read data using python

 
 
  1. # Coding = gbk
  2. Print open ("Test.txt"). read ()
  3. Result: abc (Chinese)

The file format into UTF-8:
Result: abc Juan
Obviously, decoding is required here:

 
 
  1. # Coding = gbk
  2. Import codecs
  3. Print open ("Test.txt"). read (). decode ("UTF-8 ")
  4. Result: abc (Chinese)

I used Editplus to edit test.txt, but when I used the notepad that came with Windows to edit and coexist in UTF-8 format,
Running error:
Originally, some software, such as notepad, will insert three invisible characters 0xEF 0xBB 0xBF at the beginning of the file when saving a file encoded in UTF-8 ).
Therefore, we need to remove these characters during reading. The codecs module in python Chinese defines this constant:

 
 
  1. # Coding = gbk
  2. Import codecs
  3. Data = open ("Test.txt"). read ()
  4. If data [: 3] = codecs. BOM_UTF8:
  5. Datadata = data [3:]
  6. Print data. decode ("UTF-8 ")
  7. Result: abc (Chinese)

3) A few issues left behind

In the second part, we use the unicode function and decode method to convert str to unicode. Why do the parameters of these two functions use "gbk?
The first response is that gbk (# coding = gbk) is used in our encoding statement, but is it true?
Modify the source file:

 
 
  1. # Coding = UTF-8
  2. S = "Chinese"
  3. Print unicode (s, "UTF-8 ")
  4. Run, error:
  5. Traceback (most recent call last ):
  6. File "ChineseTest. py", line 3, in <module>
  7. S = unicode (s, "UTF-8 ")
  8. UnicodeDecodeError: & apos; utf8 & apos; codec can & apos; t decode
    Bytes in position 0-1: invalid data

To put it simply, print in python directly transmits the string to the operating system, so you need to decode str to the same format as the operating system. CP936 (almost the same as gbk) is used in Windows, so gbk can be used here.
Last test:

 
 
  1. # Coding = UTF-8
  2. S = "Chinese"
  3. Print unicode (s, "cp936 ")
  4. Result: Chinese

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.