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

Python入门之中文乱码

时间:2015-10-07 22:45:36      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

python入门编码:

Python 文件中如果未指定编码,有中文代码时候,在执行过程会出现报错:

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

  

Python中默认的编码格式是 ASCII 格式,在没修改编码格式时无法正确打印汉字,所以在读取中文时会报错。

解决方法为只要在文件开头加入 # -*- coding: UTF-8 -*- 或者 #coding=utf-8 就行了。

 

所以如果大家再学习过程中,代码中包含中文,就需要在头部指定编码。

注意:Python3.X 源码文件默认使用utf-8编码,所以可以正常解析中文,无需指定 UTF-8 编码。

 

Python入门之中文乱码

标签:

原文地址:http://www.cnblogs.com/spring87/p/4859649.html

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