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类型的处理的更多相关文章

  1. Python 3中的str和bytes类型

    Python3 中的str和bytes类型 Python3最重要的新特性之一是:对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Pyt ...

  2. Python str 与 bytes 类型(Python2/3 对 str 的处理)

    本文均在 Python 3 下测试通过,python 2.x 会略有不同. 1. str/bytes >> s = '123' >> type(s) str >> ...

  3. python之路day06--python2/3小区别,小数据池的概念,编码的进阶str转为bytes类型,编码和解码

    python2#print() print'abc'#range() xrange()生成器#raw_input() python3# print('abc')# range()# input() = ...

  4. Python str 与 bytes 类型 之间的转换

    bytes:字节数组,通常用它可以描述 “一个字符串”,只不过该字符串是  “bytes类型”,所以容易与str类型混淆,他们二者之间的转换: https://blog.csdn.net/lanchu ...

  5. 关于python2中的unicode和str以及python3中的str和bytes

    python3有两种表示字符序列的类型:bytes和str.前者的实例包含原始的8位值:后者的实例包含Unicode字符. python2中也有两种表示字符序列的类型,分别叫做str和unicode. ...

  6. 三目运算的使用&bytes类型转str类型

    一.三目运算的使用 就像c语言中有三目运算符一样,python中也有三目运算符,废话不多说直接上代码 a=3 c=4 b=a if a>c else c print(b) 意思就和 if a&g ...

  7. python2与python3的区别 ,小数据池 bytes 类型

    一.python2和3的区别 在python3中 在python2中 print('ab')方式打印内容()括号是必须要有的.   print 'ab' 可以加可以不加. 只有range   有ran ...

  8. TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法

    Python2随机写入二进制文件: with open('/python2/random.bin','w') as f: f.write(os.urandom(10)) 但使用Python3会报错: ...

  9. 由time.tzname返回值引发的对str、bytes转换时编码问题实践

    Windows 10家庭中文版,Python 3.6.4, 下午复习了一下time模块,熟悉一下其中的各种时间格式的转换:时间戳浮点数.struct_tm.字符串,还算顺利. 可是,测试其中的time ...

随机推荐

  1. php sleep函数延迟执行

    PHP sleep函数一般用于定时执行任务中,表示延迟多少秒在执行程序.这里主机吧主要给大家讲一下sleep函数的语法和应用实例. sleep函数语法: sleep(seconds); //secon ...

  2. 使用python函数持续监控电脑cpu使用率、内存、c盘使用率等

    方法一: # import time 导入time模块 # import psutil 导入psutil模块 # def func(): # while True: ------->持续监控得w ...

  3. 团队第六次 # scrum meeting

    github 本此会议项目由PM召开,召开时间为4-10日晚上9点 召开时长20分钟 任务表格 袁勤 负责协调前后端 https://github.com/buaa-2016/phyweb/issue ...

  4. MySQL面试试题与答案

    本次试题设计两个表:student.exam student表 exam表 一.写一条SQL语句,按学号排序输出数学成绩 SELECT s.sno sno,score FROM exam e,stud ...

  5. 在eclipse下,用Maven创建Spring MVC工程

    参考链接:https://www.cnblogs.com/yangyxd/p/5955630.html 1.打开Eclipse,Ctrl + N  创建Maven

  6. SQLALchemy中关于复杂关系表模型的映射处理

    映射在第五步,我们还是一步一步来哈 一. 关系介绍 举一个比较经典的关系,部门与员工(以下是我的需求情况,算是把该有的关系都涉及到了) 1.每个部门会有很多成员(这里排除一个成员属于多个部门的情况) ...

  7. JPA报错问题修改小结

    项目中在使用线程跑定时任务时,遇到报错,"Could not open JPA EntityManager for transaction Caused by: org.hibernate. ...

  8. redis伪集群脚本

    #安装redis伪集群脚本,先把redis-..gem及启动脚本放在/data1/redis-cluster目录下,然后执行该脚本即可 #!/bin/bash set -e #获取redis本机ip ...

  9. 吴裕雄 python 机器学习——ElasticNet回归

    import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.mplot ...

  10. 7、...arg ...[1,2,3] 数组扩展

    1.将离散的参数转成数组 2.将数组拆成单个离散的值 https://blog.csdn.net/qq_30100043/article/details/53391308 箭头函数写法 函数名 -&g ...