python encode decode unicode区别及用法
2013-12-20 11:29
169 查看
decode解码
encode转码
unicode是一种编码,具体可以百度搜
encode转码
unicode是一种编码,具体可以百度搜
#coding:UTF-8 u=u'汉' printrepr(u)#u'\u6c49' s=u.encode('UTF-8') printrepr(s)#'\xe6\xb1\x89' u2=s.decode('UTF-8') printrepr(u2)#u'\u6c49' #对unicode进行解码是错误的 #s2=u.decode('UTF-8') #同样,对str进行编码也是错误的 #u2=s.encode('UTF-8')
s=u.encode('UTF-8')是把u转码成utf-8
u2=s.decode('UTF-8')是把u解码成utf-8 如果是windows下编码一般是gbk,所以解码时候要用u.decode('gbk'),如下
>>>u='格式' >>>u.decode('gbk') u'\u683c\u5f0f' >>>u.decode('utf-8') Traceback(mostrecentcalllast): File"<pyshell#111>",line1,in<module> u.decode('utf-8') File"C:\Python27\lib\encodings\utf_8.py",line16,indecode returncodecs.utf_8_decode(input,errors,True) UnicodeDecodeError:'utf8'codeccan'tdecodebyte0xb8inposition0:invalidstartbyte >>>
相关文章推荐
- python:MySQLdb把查询记录变成像php那样的字典
- Python Errors and Exceptions
- python——执行cmd命令
- python模块学习---mechanize(模拟浏览器)
- Web自动化测试(3): Selenium Web Driver 如何操作web页面。
- Scikit Learn: 在python中机器学习
- IDE Python
- 【Python】之 Openers和Handlers
- python科学计算五:sklearn学习
- Python2.X内置函数学习
- Python单例模式的4种实现方法
- Python脚本控制的WebDriver 常用操作 <二十八> 超时设置和cookie操作
- Python 学习笔记(1)
- Python脚本控制的WebDriver 常用操作 <二十七> 文件下载
- python Base64编码
- Python基础教程---读书笔记二
- Python 学习笔记 (4)—— 模块
- Python 学习笔记 (4)—— 模块
- python 学习笔记2------概述2
- Python 学习入门—— 正则表达式 转载