Python solution to Chinese garbled problem under eclipse

Source: Internet
Author: User
Tags ini

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.

Related Article

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.