Python appears syntaxerror:non-ascii character ' \xe6 ' in file print date. py on line 1, but no encoding declared;

Source: Internet
Author: User

Just start learning Python and follow the code in the book:

#根据给定的年月日以数字的形式打印出来months = [     ' January ',     ' Febuary ',     ' March ',     ' April ',     ' may ',     ' June ',     ' July ',     ' August ',     ' September ',     ' October ',     ' November ',     ' December ']# With 1~31 number as the end of the list endings = [' st ', ' nd ', ' Rd '] + + * [' th ']         + [' st ', ' nd ', ' Rd '] + 7 * [' th ']         + [' st ']year    = Raw_input (' Year: ') Month   = Raw_input (' Month (1-12): ') Day     = Raw_input (' Day (1-31) ') month_number = Int (month) day_number = Int (day) #记得要将月份和天数减1 to get the correct index month_name = months[month_number-1]ordinal = day + endings[day_number-1] Print Month_name + ' + ordinal + ', ' + year~                                           

An error occurred while executing:

Syntaxerror:non-ascii character ' \xe6 ' in file print date. py on line 1, but no encoding declared; See http://www.python.org/peps/pep-0263.html for details


Baidu a bit:

The discovery is because Python does not support encoding in the source file by default. The solution has the following three kinds:

First, add the following comment code at the head of the file:

# coding=<encoding Name> For example, you can add # Coding=utf-8

Second, add the following two lines of comment code at the head of the file:

#!/usr/bin/python

#-*-Coding: <encoding name>-*-For example, you can add #-*-Coding:utf-8-*-

Third, add the following two lines of comment code at the head of the file:

#!/usr/bin/python

# Vim:set fileencoding=<encoding name>: For example, you can add # Vim:set Fileencoding=utf-8:


I used the first method to solve! Record it.


Python appears syntaxerror:non-ascii character ' \xe6 ' in file print date. py on line 1, but no encoding declared;

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.