Python2和3版本对str和bytes类型的处理
python2中字符串分为2种类型:
字节类型:str,字节类型,通过decode()转化为unicode类型
unicode类型:unicode ,通过encode转化为str字节类型
字节类型 和 unicode类型的转化:
- 字节类型通过decode转化为unciode类型
- unciode类型通过encode方法转化为直接类型
- 方法的使用和python3相同,但是在方法中默认的编码方式为ascii, 对中文需要手动指定为utf-8
python3中字符串分为2种类型:
str:unicode,通过encode() 转化为bytes
bytes:字节类型,通过decode()转化为 str类型
1. str/bytes
Python 3 所有的 strings 均是 unicode 类型。
Python 2 将 strings 处理为原生的 bytes 类型,而不是 unicode。
# python3 中
# python3 中 >>> a = '中文' >>> a
'中文' >>> type(a)
<class 'str'>
# python2中,由于a已经是字节类型,所以只能对其进行解码变为str类型,不能对其进行编码.(根据2.x版本不同,有时候也不能进行解码)
# python2中 >>> a = '中文' >>> a
'\xd6\xd0\xce\xc4' >>> type(a)
<type 'str'> # 相当于 b"中文" 这个写法
>>> b = b"中文" >>> b
'\xd6\xd0\xce\xc4' >>> type(b)
<type 'str'>
2. str 与 bytes 之间的类型转换
str 与 bytes 之间的类型转换如下:
# str 与 bytes 之间的类型转换如下:
str -> bytes: bytes(s, encoding='utf8')
bytes -> str: str(b, encoding='utf-8') # 通过编码解码的形式对二者进行转换
str 编码成 bytes 格式: str.encode(s)
bytes 格式编码成 str 类型: bytes.decode(b)
另附: python str与bytes之间的转换
# bytes object
b = b"example" # str object
s = "example" # str to bytes
bytes(s, encoding = "utf8") # bytes to str
str(b, encoding = "utf-8") # an alternative method
# str to bytes
str.encode(s) # bytes to str
bytes.decode(b)
Python2和3版本对str和bytes类型的处理的更多相关文章
- Python 3中的str和bytes类型
Python3 中的str和bytes类型 Python3最重要的新特性之一是:对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Pyt ...
- Python str 与 bytes 类型(Python2/3 对 str 的处理)
本文均在 Python 3 下测试通过,python 2.x 会略有不同. 1. str/bytes >> s = '123' >> type(s) str >> ...
- python之路day06--python2/3小区别,小数据池的概念,编码的进阶str转为bytes类型,编码和解码
python2#print() print'abc'#range() xrange()生成器#raw_input() python3# print('abc')# range()# input() = ...
- Python str 与 bytes 类型 之间的转换
bytes:字节数组,通常用它可以描述 “一个字符串”,只不过该字符串是 “bytes类型”,所以容易与str类型混淆,他们二者之间的转换: https://blog.csdn.net/lanchu ...
- 关于python2中的unicode和str以及python3中的str和bytes
python3有两种表示字符序列的类型:bytes和str.前者的实例包含原始的8位值:后者的实例包含Unicode字符. python2中也有两种表示字符序列的类型,分别叫做str和unicode. ...
- 三目运算的使用&bytes类型转str类型
一.三目运算的使用 就像c语言中有三目运算符一样,python中也有三目运算符,废话不多说直接上代码 a=3 c=4 b=a if a>c else c print(b) 意思就和 if a&g ...
- python2与python3的区别 ,小数据池 bytes 类型
一.python2和3的区别 在python3中 在python2中 print('ab')方式打印内容()括号是必须要有的. print 'ab' 可以加可以不加. 只有range 有ran ...
- TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法
Python2随机写入二进制文件: with open('/python2/random.bin','w') as f: f.write(os.urandom(10)) 但使用Python3会报错: ...
- 由time.tzname返回值引发的对str、bytes转换时编码问题实践
Windows 10家庭中文版,Python 3.6.4, 下午复习了一下time模块,熟悉一下其中的各种时间格式的转换:时间戳浮点数.struct_tm.字符串,还算顺利. 可是,测试其中的time ...
随机推荐
- 【转录组入门】3:了解fastq测序数据
操作:需要用安装好的sratoolkit把sra文件转换为fastq格式的测序文件,并且用fastqc软件测试测序文件的质量 作业:理解测序reads,GC含量,质量值,接头,index,fastqc ...
- C# 时钟控件
//控件名:myNewClock //作者:刘典武 //时间:2011-06-10 using System; using System.Collections.Generic; using Syst ...
- java Page分页显示
//entity层实体类 import java.util.List; //分页展示 //相关属性:当前页,页大小(每页显示的条数),总页数,总条数,数据 //select * from t_user ...
- MySQL表数据的增删改查
1.增 INSERT INTO tb VALUES(v1,v2,v3...); INSERT INTO tb (field1,field2...) VALUES(v1,v2...); INSERT I ...
- 单片机课程设计-四位加法计算器设计参考程序(c语言)
#include<reg52.h> typedef unsigned char uint8; typedef unsigned int uint16; sbit rw=P2^; sbit ...
- spring boot application.properties详解
附上最新文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-propertie ...
- 用户未登录或Session超时时重定向到登录页,不那么简单
在网站开发中,我们经常有这样的场景出现: 情景1:对未登录的用户或没有权限的用户,当其想访问某个受限网页时,系统要能够自动转到登录页面. 情景2:对于用session保存用户状态的情况还有这样一种 ...
- nodeJs 控制台打印中文显示为Unicode解决方案
在使用 NodeJs 采集其他网站网页时遇到的,在获取源代码后发现里面原来的中文被转成了 Unicode(UTF8) 编码的中文(如:&# [xxx]),这当然不是真正想要的中文实体 解决方案 ...
- gitlab安装部署汉化
1.获取gitlab汉化包(要部署非汉化版,可以跳过这一块内容) 说明:gitlab中文社区版的项目,v7-v8.8是由Larry Li发起的“GitLab 中文社区版项目”(https://gitl ...
- linux随机字符串
pwgen -s 32 1 openssl passwd -salt openssl rand -base64 32 openssl rand -hex 100