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

python编码处理:unicode字节串转成中文 各种字符串举例说明

时间:2014-11-18 23:11:20      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:blog   http   使用   sp   strong   数据   on   问题   log   

编码问题一直是很头痛的问题:

 

当字符串是:‘\u4e2d\u56fd‘ 

>>>s=[‘\u4e2d\u56fd‘,‘\u6e05\u534e\u5927\u5b66‘]
>>>str=s[0].decode(‘unicode_escape‘)  #.encode("EUC_KR")
>>>print str

中国

 

当字符串是:‘ 东亚学团一中‘

>>>print unichr(19996)

ord()支持unicode,可以显示特定字符的unicode号码,如:

>>>print ord(‘A‘)
65

只要和Unicode连接,就会产生Unicode字串。如:

>>> ‘help‘
‘help‘
>>> ‘help,‘ + u‘python‘     
u‘help,python‘

 

对于ASCII(7位)兼容的字串,可和内置的str()函数把Unicode字串转换成ASCII字串。如:

>>> str(u‘hello world‘)
‘hello world‘

 

 

对几个概念的理解:

ASCII码:    用数据字 对应 相应的字符

bubuko.com,布布扣

 

而中文 就是区位码对应汉字    如:“好” 的ASCII码为: 22909

 

unicode 编码 每个国家分一块。   它有UTF-8、UTF-16、UTF-32等形式

中文范围 4E00-9FBF:  这个范围内有 gbk,gb2312,

 

utf-8是基于unicode的 国际化的场合适合使用 
gb2312和gb2312都是国标码 出现的较早 主要用于编解码常用汉字

python编码处理:unicode字节串转成中文 各种字符串举例说明

标签:blog   http   使用   sp   strong   数据   on   问题   log   

原文地址:http://www.cnblogs.com/liyuzhao/p/4106255.html

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