Python中使用中文字符串

来源:互联网 发布:淘宝薇薇家叶罗丽娃娃 编辑:程序博客网 时间:2024/05/16 15:47

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' )

原创粉丝点击