python学习——编码
时间:2014-04-29 13:12:20
收藏:0
阅读:406
为了将各种不同的语言都包含在同一的字符集中,满足国际间的信息交流国际上制定了UNICODE字符集。
通过使用UNICODE字符集可以满足跨语言的文字处理,有效的避免乱码产生。
使用方法:在脚本中增加以下代码行之一
- #-*- coding:utf-8 -*-
- #coding:utf-8
注意:如果是在命令行输出中文,则需要设定编码为cp936,文件格式保存为utf-8编码
示例代码:
#coding:utf-8 ch = ‘在中使用中文需要注意字符编码问题‘ print ch print ch.decode(‘utf-8‘).encode(‘cp936‘)
执行结果:
在eclipse下执行:

在dos窗口下执行:

原因:windows命令行采用的是cp936 ,脚本采用utf-8编码导致乱码。在脚本中才用decode和encode函数对字符重新解码编码,或者,不将其保存为uft-8格式
评论(0)