码迷,mamicode.com
首页 > 编程语言 > 详细

python出现SyntaxError: Non-ASCII character '\xe6' in file错误

时间:2015-08-10 02:08:23      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:character   declared   february   details   python   

SyntaxError: Non-ASCII character ‘\xe6‘ in file ./1.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details


百度查了一下是因为Python在默认状态下不支持源文件中的编码所致。

在文件头部添加如下两行注释码:

 #!/usr/bin/env python

# vim: set fileencoding=<encoding name> : 例如,可添加# vim: set fileencoding=utf-8 :


以下是修正后完整代码:

#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
#根据给定的年月日以数字形式打印出日期
months = [
        ‘january‘,
        ‘february‘,
        ‘march‘,
        ‘april‘,
        ‘may‘,
        ‘june‘,
        ‘july‘,
        ‘august‘,
        ‘september‘,
        ‘october‘,
        ‘november‘,
        ‘december‘
]

#以1~31的数字作为结尾的列表
endings = [‘st‘, ‘nd‘, ‘rd‘] + 17 * [‘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,以获得正确索引
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]

print month_name + ‘ ‘ + ordinal + ‘ , ‘ + year

python出现SyntaxError: Non-ASCII character '\xe6' in file错误

标签:character   declared   february   details   python   

原文地址:http://guangzhao95.blog.51cto.com/7948111/1683169

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!