python 字符串编码与解码
来源:互联网 发布:演化算法 编辑:程序博客网 时间:2024/05/16 11:53
1. 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。
decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码。
encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码。
因此,转码的时候一定要先搞明白,字符串str是什么编码,然后decode成unicode,然后再encode成其他编码
str = "中国"
print str --->中国
直接 str --- >'\xe4\xb8\xad\xe5\x9b\xbd'
原因:str并不是unicode码( isinstance (s,unicode) --> false )
2. 如何获得系统默认编码?
sys.getdefaultencoding()
0 0
- python 字符串编码与解码
- Python字符串的编码与解码
- python 编码与解码
- Python---解码与编码
- Python字符串的编码与解码(encode与decode)
- Python字符串的编码与解码(encode与decode)
- Python字符串的编码与解码(encode与decode)
- 字符串的编码与解码
- Java字符串编码与解码
- Python的编码与解码
- Python 编码与解码1
- Python中的编码与解码
- python 的编码与解码
- 编码与解码(python 经验)
- python-url编码与解码
- Python字符串的编码和解码
- 字符串的(编码/加密)与(解码/解密)
- 字符串编码与解码函数(UTF8)
- Java对象入门
- POJ1274(多重背包)
- Matlab 绘制地图各种属性设置
- 临时文件
- android 本地html JS与android 交互
- python 字符串编码与解码
- [LeetCode]Binary Tree Level Order Traversal
- Yii 中,render 和 renderPartial 的区别。
- 柳岩黄灿盛因戏生情 柳岩甩奶舞不雅照亮瞎眼
- 如何访问Google
- 标量子查询中exist转化成外连接
- DWR入门教程
- 【翻译自mos文章】得到正在运行job的 session id
- java--struts2--action传值到jsp