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? 是因为要表示图片.视频等二进制格式 ...
随机推荐
- C语言字符串复制
strcpy(arg1,arg2);//将arg2内容赋值到arg1 strncpy(arg1,arg2,size);//赋值多少由size决定,如果要截取某一部分,可以将arg2指针进行arg2+x ...
- Django组件——Cookie与session相关
一,会话跟踪技术 1 什么是会话跟踪技术我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而1 ...
- numpy知识点总结
一.数组方法 创建数组:arange()创建一维数组:array()创建一维或多维数组,其参数是类似于数组的对象,如列表等 反过来转换则可以使用numpy.ndarray.tolist()函数,如a. ...
- oralce 日期 date 相关操作
1.当前时间加减一年 加一年 select sysdate,add_month(sysdate,12) from dual; 减一年 select sysdate,add_month(sysdate, ...
- Sass-数据类型
Sass和JavaScript语言类似,也具有自己的数据类型,在Sass中包含一下几种数据类型 数字:如,1,2,13,10px; 字符串: 有引号字符串或无引号字符串,如,“foo”,"b ...
- Flutter pubspec.yaml配置文件
name: flutter_app1 # 应用名称 description: A new Flutter application. # 应用描述 # The following defines the ...
- BZOJ2695 保护古迹
非常带劲之计算几何 写的头晕= = 就是平面图转对偶图然后最小割 由于p非常小我们枚举所有保护状态然后割一下 建图真的烦 就是把区域划分出来看一下每一个古迹点是否被小区域包含[好像也可以写点定位] 然 ...
- 咱们从头到尾讲一次 Flink 网络流控和反压剖析
本文根据 Apache Flink 系列直播整理而成,由 Apache Flink Contributor.OPPO 大数据平台研发负责人张俊老师分享.主要内容如下: 网络流控的概念与背景 TCP的流 ...
- 看图了解RocksDB
它是一个高性能的Key-Value数据库.设计了完善的持久化机制,同时保证性能和安全性.能够良好的支持范围查询,因为K-V记录就是按照Key来排序的. 下图为写入的流程: 可以看到主要的三个组成部 ...
- springmvc REST风格的URL
1:需要配置一个filter <!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELET ...