Python in Windows Chinese display

Source: Internet
Author: User

See Mechanize today, on the Internet to find examples of experiments, found that as long as the code appears in Chinese, will be error

' \xe4 ' inch 2 , but no encoding declared; see http: // python.org/dev/peps/pep-0263/for Details

Python's default encoding file is the ASCII code used

If the Python file uses non-English characters such as Chinese, it will be an error.

How to Solve

In the first line of the Python source file, add

# coding=utf-8

Or

#-*-coding:utf-8 -*-

Output

#-*-coding:utf-8 -*-string=' hello '  string

Switch

#-*-coding:utf-8 -*-string=' hello '     String. Decode ('UTF-8'

Chinese strings are stored in tuples, lists, or dictionaries, and you can call the dumps method of the JSON module

#-*-coding:utf-8-*-Import JSON print u'Hello' string='Hello'Printstring. Decode ('UTF-8') T_tuple=('Hey','Hello') T_list=['Hey','Hello'] T_dict={1:'Hey',2:'Hello'} print json.dumps (t_tuple,encoding='UTF-8', ensure_ascii=False) Print json.dumps (t_list,encoding='UTF-8', ensure_ascii=False) Print json.dumps (t_dict,encoding='UTF-8', Ensure_ascii=false)

Python in Windows Chinese display

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.