unicode,decode,encode在python的作用
来源:互联网 发布:上海软件项目经理工资 编辑:程序博客网 时间:2024/05/16 23:40
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。
即:其他编码->解码(decode)->unicode->编码(encode)->其他编码2
decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码。
encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码。
代码中字符串的默认编码与代码文件本身的编码一致。
如:s='中文'
如果是在utf8的文件中,该字符串就是utf8编码,如果是在gb2312的文件中,则其编码为gb2312。这种情况下,要进行编码转换,都需要先用decode方法将其转换成unicode编码,再使用encode方法将其转换成其他编码。通常,在没有指定特定的编码方式时,都是使用的系统默认编码创建的代码文件。
如果字符串是这样定义:s=u'中文'
则该字符串的编码就被指定为unicode了,即python的内部编码,而与代码文件本身的编码无关。因此,对于这种情况做编码转换,只需要直接使用encode方法将其转换成指定编码即可。
阅读全文
0 0
- unicode,decode,encode在python的作用
- Unicode Python encode decode的知识点总结
- python中decode,encode跟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中 encode,decode,unicode,utf-8
- python中的编码问题unicode, encode, decode
- python的decode和encode
- python的decode和encode
- python 的encode和decode
- decode、encode、unicode解码
- python 编码转换 unicode和encode作用
- python的str,unicode对象的encode和decode方法(转)
- 二维数组和vector实现dijkstra
- Vue, App与我(六)
- jQuery插件开发
- Content Provider之间互相通信的源码浅析
- MySQL加强170725
- unicode,decode,encode在python的作用
- 【JavaSE系列—基础篇7】——注解基础知识
- 修改UBuntu的文件夹权限
- nginx配置与优化
- 2017.07.27am-初学Javascript的小笔记
- mybatis学习笔记
- String类——常见字符串操作指令
- android ndk error :undefined reference to.
- QtableView选择范围操作 通过MAP存储选择行 获取最上行号和最下行号 上移下移一行等一系列操作