Character set encoding and file read and write implementation program in Python

Source: Internet
Author: User
Tags character set in python

The default encoding in Python is ASCII, which can be set and obtained in the following ways:

Import Sys
Print sys.getdefaultencoding ()
Sys.setdefaultencoding (' GBK ') but the new default encoding will not take effect until Python restarts, I tried, and setdefaultencoding always went wrong, without this attribute. With Dir, there is no, the Python version is 2.5 and I don't know if it was canceled.

When you use Print to output, Python passes the content to system processing, and windows outputs it according to the system default encoding. If you include Chinese, you should pay attention to a few points.

1 encoding of Python code files
The py file is ASCII encoded by default, and the Chinese will do an ASCII to system default encoding conversion when it is displayed: Syntaxerror:non-ascii character. You need to add encoding instructions in the first or second line of the code file:

The code is as follows Copy Code
1 # CODING=GBK
2 print ' Chinese '
2 Encoding of strings


Strings entered directly like the one above are processed according to code file encoding, and in the case of Unicode encoding, there are three ways to do this:

The code is as follows Copy Code
1 s1 = U ' Chinese '
2 s2 = Unicode (' Chinese ', ' GBK ')
3 S3 = S1.decode (' GBK ')


Unicode is a built-in function, and the second parameter indicates the encoding format of the source string.
Decode is a method of any string that converts a string to Unicode format, and a parameter indicates the encoding format of the source string.
Encode is also a method of any string that converts a string into a format specified by the parameter.

3 default encoding of the system
For the Chinese system, the default is gbk,gb2312, because it is a GBK word set. When you use print output, strings are converted to this format, when implicitly converted, from the code file encoding format to GBK, which is ASCII->GBK by default. Consider the 2nd above, if the string encoding is not ASCII, then the implicit conversion will be an error, and an explicit conversion is required, using the Encode method. If the code file format is specified as GBK, there is no problem with the implicit conversion.

The code is as follows Copy Code
1 # CODING=GBK
2
3 s = U ' Chinese '
4 Print S.encode (' GBK ')


File Read and Write

--------------------------------------------------------------------------------
Only ASCII or GBK encoded file read and write, relatively simple, read and write as follows:

The code is as follows Copy Code
1 # CODING=GBK
2
3 f = open (' C:/intimate.txt ', ' R ') # R indicates file open mode, that is, read-only
4 S1 = F.read ()
5 s2 = f.readline ()
6 s3 = F.readlines () #读出所有内容
7
8 F.close ()
9
f = open (' C:/intimate.txt ', ' W ') # W Write file
One f.write (S1)
F.writelines (S2) # no WriteLine
F.close ()

F.writelines does not output line breaks.
Unicode file read and write:

  code is as follows copy code
1 # CODING=GBK
 2 Import codecs
 3
 4 f = codecs.open (' C:/intimate.txt ', ' a ', ' utf-8 ')
 5 f.write (U ') Chinese ')
 6 s = ' Chinese '
 7 f.write (S.decode (' GBK '))
 8 f.close ()
 9
Ten f = codecs.open (' C:/intimate.txt ', ' r ', ' Utf-8 ')
One s = F.readlines ()
F.close ()
for line in S:
14   &nbs P Print Line.encode (' GBK ')

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.