python2和python3编码
python2编码
unicode:unicode 你好 u'\u4f60\u597d'
| | | |
encode('utf8')| |decode('utf8') encode('gbk')| |decode('gbk')
| | | |
utf8 gbk
编码后的str '\xe4\xbd\xa0\xe5\xa5\xbd' 编码后的gbk u'\u6d63\u72b2\u30bd'
# str: bytes
>>> s = '你好 world'
>>> print repr(s)
'\xe4\xbd\xa0\xe5\xa5\xbd world'
>>> print len(s)
12
>>> print type(s)
<type 'str'>
# unicode:unicode
>>> s = u'你好 world'
>>> print repr(s)
u'\u4f60\u597d world'
>>> print len(s)
8
>>> print type(s)
<type 'unicode'>
#unicode: 无论什么字符在Unicode都有一个对应。
python2的特点
1.在python2中print把字节转成了Unicode
2.python2中以默认已ASCII编码
[root@localhost ~]# cat python.py
#coding:utf8 # 告诉解释器以utf8编码
print '你好'
python3编码
在python3中默认以utf8编码
str:unicode 你好 u'\u4f60\u597d'
| | | |
encode('utf8')| |decode('utf8') encode('gbk')| |decode('gbk')
| | | |
utf8 gbk
编码后的str '\xe4\xbd\xa0\xe5\xa5\xbd' 编码后的gbk u'\u6d63\u72b2\u30bd'
>>> s = '你好 world'
>>> print (json.dumps(s))
"\u4f60\u597d world"
>>> print (len(s))
8
>>> print (type(s))
<class 'str'>
编码解码方式1:
>>> s = '你好 world'
>>> b = s.encode('utf8')
>>> print (b)
b'\xe4\xbd\xa0\xe5\xa5\xbd world'
>>> s = b.decode('utf8')
>>> print (s)
你好 world
>>> s = b.decode('gbk')
>>> print (s)
浣犲ソ world
编码解码方式2:
>>> s = '你好 world'
>>> b = bytes(s,'gbk')
>>> print (b)
b'\xc4\xe3\xba\xc3 world'
>>> s = str(b,'gbk')
>>> print (s)
你好 world >>> s = '你好 world'
>>> b = bytes(s,'utf8')
>>> print (b)
b'\xe4\xbd\xa0\xe5\xa5\xbd world'
>>> s = str(b,'utf8')
>>> print (s)
你好 world
>>> s = str(b,'gbk')
>>> print (s)
浣犲ソ world
python2和python3编码的更多相关文章
- python2和python3编码问题
欢迎加入python学习交流群 667279387 一.什么是编解码 1.什么是unicode 2.编码方式 二.python中的编解码 1.python2 (1).encode() 和 .decod ...
- 字符编码、python2和python3编码的区别
目录 字符编码 文本编辑器存储信息的过程 python解释器解释python代码的流程 python解释器与文本编辑器的异同 不同编码格式存入与读取数据的过程 乱码的分析 python2和python ...
- Python2 和 Python3 编码问题
基本存储单元 位(bit, b):二进制数中的一个数位,可以是0或者1,是计算机中数据的最小单位. 字节(Byte,B):计算机中数据的基本单位,每8位组成一个字节. 1B = 8b 各种信息在计算机 ...
- python2与python3编码
#coding:utf8#一#1.在python2中,默认以ASCII编码chcp 936import sysprint sys.getdefaultencoding()# ascii#str:byt ...
- Python2和Python3编码的区别
Python2 python2中有两种储存变量的形式,第一种:Unicode:第二种:按照coding头来的. 假设python2用utf8存储x='中文',当你print(x)的时候,终端接收gbk ...
- python2与python3编码问题
python2: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 33: 解决办法: 在报错的页面添加代码: ...
- python2与python3编码(练习)
#_author:来童星#date:2019/12/9import jsons='star'a=s.encode('utf8')print(s,type(s))# star <class 'st ...
- pickle 在python2 to python3 编码出现错误
pickle.load(file) UnicodeDecodeError: 'ascii' codec can't decode byte 0xf5 in position 2: ordinal no ...
- Python全栈之路----Python2与Python3
金角大王Alex python 之路,致那些年,我们依然没搞明白的编码 python2与python3的区别 py2 str = bytes 为什么有bytes? 是因为要表示图片.视频等二进制格式 ...
随机推荐
- shell变量的声明和使用
- Jumpserver安装过程
Jumpserver 安装过程 可参照此官方文档搭建: http://docs.jumpserver.org/zh/docs/step_by_step.html 其中,需注意处: # docker ...
- ed-tue-robotics
https://github.com/tue-robotics/ed ubuntu16.04 安装libsdformat4-dev ,libsdformat4 1./usr/include/sdfor ...
- 二 shell 基础
一 文件的 权限基础 文件有三类权限 user,group,other, 权限分为 r w x 代表数字分别为 4 2 1 修改权限命令 chmod 权限还有特殊权限,在执行的时候代表某一身 ...
- weblogic启动脚本01
DATE=`date +%Y%m%d%H%M%S` user=`whoami` logDir=/app/logs/sguap_admin #启动日志存放路径sguap是例子系统简称# logDestd ...
- SOA架构是什么?
https://blog.csdn.net/u013343616/article/details/79460398 SOA是什么?SOA全英文是Service-Oriented Architectur ...
- Using If/Truth Statements with pandas
pandas follows the numpy convention of raising an error when you try to convert something to a bool. ...
- h5视频做背景的样式
video{ position: fixed; display: block; width: 100%; object-fit:fill; height:100%; right: 0px; botto ...
- DZY Loves Chemistry
DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- redis哨兵-5
#地址: https://www.cnblogs.com/PatrickLiu/p/8444546.html #常用架构 redis1主1从+3哨兵 实现redis高可用 #redis主从 ##### ...