About editing environment encoding
The first thing to ensure is that the Eclipse Editor environment is encoded as UTF8, which is a major premise; second, if the py file contains Chinese characters, it needs to be in the py file
The encoding is declared in.
1. Modify Eclipse Editor encoding
A) Window->preferences->general->editors->text Editors->spelling->encoding->utf-8
b) window->preferences->workspace->text file Encoding->utf-8
C Open Eclipse installation directory->eclipse.ini, last line plus "-dfile.encoding=utf-8"
2. Modify Python file encoding statement
In the first line of the Py file, add #coding=utf-8, and make sure you have the first line
About page character encoding
Complete the above two points, there is no problem with the local Chinese character processing. But sometimes we need to deal with Chinese characters on the page, such as reading a Web page,
Page content to crawl, then just do the above two points is not enough. We need to use decode and encode.
Decode and encode are a pair of opposing methods. Decode is a decoding, such as a Web page using the GB2312 encoding format, then read () method reads
After the content of the page, using Str.decode (' gb2312′ ') is to decode the content of the page into Unicode encoding; encode on the contrary, it is
Unicode encoding is the specified format, if previously decoded by Decode to Unicode,str.encode (' utf8′ ') to encode str as UTF8
Expression
So naturally we have a way to properly display the contents of a Web page in eclipse. For example, the gb2312 format used in the Web page, the Eclipse environment has been adjusted
For UTF8 encoding, if the direct read () comes out, the display in Eclipse is garbled, and if you add a procedure after read (), the display is normal, as
Under
The code is as follows |
Copy Code |
html = F.read (). Decode (' gb2312 '). Encode (' UTF8 ') |
Settings for eclipse+pydev2.2+python2.7
The code is as follows |
Copy Code |
Window->preferences->general->editors->text Editors->spelling->encoding->utf-8 Window->preferences->workspace->text file Encoding->utf-8
|
Open Eclipse installation directory->eclipse.ini, last line plus "-dfile.encoding=utf-8"
File encoding
The py file is remembered as UTF-8 and the first line of the file plus "#coding =utf-8"
Run Time settings
The code is as follows |
Copy Code |
Run-->run Configurations->python run->common-> Encoding->utf-8 |
This solves the problem of Chinese garbled, if there are more methods can also be submitted under the comments Oh.