unicode .decode /str.encode方法
2005-03-21 11:26
477 查看
根据前面很多位大侠的邮件,我列了下面的表,只是还缺少一些说明,请知情者告知:下面是一些编码转换用到的方法,请问, 下表中的 (3) 和 (5) ,即str.encode 和 unicode.decode 方法是做什么用的? 能否给出例子? (1) unicode() 全局函数,将str字符串从其它编码(如gb2312)转换为unicode对象
(2) str.decode() str字符串的方法,将str字符串从其它编码(如gb2312)转换为unicode对象,与unicode()很相似.
(3) str.encode() str字符串的方法,
(4) unicode.encode() unicode对象的方法,将unicode对象转成其它相应编码的str字符串对象,如gb2312.
(5) unicode.decode() unicode对象的方法 我在此给出条目 (1), (2), (4) 方法的应用例子, 请大家补充(3),(5)的应用例子. 例子:
>>> s="中文" #str对象
>>> ss=u"中文" #unicode对象
>>> type(s)
<type 'str'>
>>> type(ss)
<type 'unicode'>
>>> s1=unicode(s,'mbcs') # str对象 --> unicode对象
>>> s1
u'/u4e2d/u6587'
>>> type(s1)
<type 'unicode'>
>>> s3=ss.encode('mbcs') # unicode对象 --> str对象
>>> s3
'/xd6/xd0/xce/xc4'
>>> type(s3)
<type 'str'>
(2) str.decode() str字符串的方法,将str字符串从其它编码(如gb2312)转换为unicode对象,与unicode()很相似.
(3) str.encode() str字符串的方法,
(4) unicode.encode() unicode对象的方法,将unicode对象转成其它相应编码的str字符串对象,如gb2312.
(5) unicode.decode() unicode对象的方法 我在此给出条目 (1), (2), (4) 方法的应用例子, 请大家补充(3),(5)的应用例子. 例子:
>>> s="中文" #str对象
>>> ss=u"中文" #unicode对象
>>> type(s)
<type 'str'>
>>> type(ss)
<type 'unicode'>
>>> s1=unicode(s,'mbcs') # str对象 --> unicode对象
>>> s1
u'/u4e2d/u6587'
>>> type(s1)
<type 'unicode'>
>>> s3=ss.encode('mbcs') # unicode对象 --> str对象
>>> s3
'/xd6/xd0/xce/xc4'
>>> type(s3)
<type 'str'>
相关文章推荐
- python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法, Python中字符编码的总结和对比bytes和str
- python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法, Python中字符编码的总结和对比bytes和str
- python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法(转)
- python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法(转)
- python的str,unicode对象的encode和decode方法(转)
- 转:python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法
- python蛋疼的编码decode、encode、unicode、str、byte的问题都在这了
- http get 请求地址传中文或值带特殊符号解决方法 URLEncoder.encode(str,"编码") new URLDecoder().decode(str,"编码")
- python 字符串编码 str和unicode 区别以及相互转化 decode('utf-8') encode('utf-8')
- 浏览器地址传中文解决方法 URLEncoder.encode(str,"编码") new URLDecoder().decode(str,"编码")
- Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
- Pyscripter下载地址及UnicodeEncodeError: 'ascii' codec解决方法
- Python字符串的encode与decode研究心得乱码问题解决方法
- Python字符串的encode与decode研究心得乱码问题解决方法