您的位置:首页 > 编程语言 > Python开发

Python中使用中文字符串

2008-11-27 00:14 483 查看
http://blog.chinaunix.net/u2/85537/showart_1423094.html

在源码中使用中文字符串,在文件头假如预处理命令就可以了:

# -*- coding: gb18030 -*-
# -*- coding: utf-8 -*-
但是如果想要在raw_input()时接收中文字符,又会遇到错误。需要对输入的中文字符进行unicode的解码,然后在进行中文字符集的编码就可以直接传到字符中去。这里的中文字符集编码可以是gbk,gb2312,gb18030都可以

text = raw_input( "Input the contents: " )
text = unicode( text, 'gbk' ).encode( 'gb18030' )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: