Python str 与 bytes 类型(Python2/3 对 str 的处理)
本文均在 Python 3 下测试通过,python 2.x 会略有不同。
1. str/bytes
>> s = '123'
>> type(s)
str
>> s = b'123'
bytes
2. 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)
3. strings 分别在 Python2、Python 3下
What is tensorflow.compat.as_str()?
Python 2 将 strings 处理为原生的 bytes 类型,而不是 unicode,
Python 3 所有的 strings 均是 unicode 类型。
Python str 与 bytes 类型(Python2/3 对 str 的处理)的更多相关文章
- Python 3中的str和bytes类型
Python3 中的str和bytes类型 Python3最重要的新特性之一是:对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Pyt ...
- python字典转bytes类型字典
python字典转bytes类型字典import base64 import json 1. a={"Vod":{"userData":"{}&quo ...
- Python2和3版本对str和bytes类型的处理
python2中字符串分为2种类型: 字节类型:str,字节类型,通过decode()转化为unicode类型 unicode类型:unicode ,通过encode转化为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 ...
- python(61):str 和 bytes 转换
str 和 bytes 转换 b = b"example" # str object s = "example" # str to bytes bytes(s, ...
- Python 3中bytes/string的区别
原文:http://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3 python 3中最重要的新特性可能就是将文 ...
- 学习python,第四篇:Python 3中bytes/string的区别
原文:http://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3 python 3中最重要的新特性可能就是将文 ...
- python中的bytes和str类型
经过一上午的查找资料.大概理清楚了bytes类型和str类型的区别. bytes类型和str类型在呈现形式有相同之处,如果你print一个bytes类型的变量,会打印一个用b开头,用单引号括起来的序列 ...
随机推荐
- 拓展Lucas小结
拓展Lucas是解决大组合数取模非质数(尤其是含平方因子的合数)问题的有力工具... 首先对模数质因数分解,把每个质因子单独拎出来处理答案,然后用中国剩余定理(excrt)合并 问题转化为,对于每个质 ...
- 05004_Linux的其他命令和权限命令
1.其他命令 (1)显示当前所在位置 命令:pwd (2)搜索命令 a.命令:grep 要搜索的字符串 要搜索的文件 示例:搜索/etc/sudu.conf文件中包含字符串to的行 b.搜索/etc/ ...
- 收集整理的openstack java封装 api的第三方实现的选择
Apache jclouds 地址:http://jclouds.apache.org/guides/openstack/ 一个开源库,java实现,支持cloudstack,openstack以及各 ...
- jquery easyui的使用
第一步下载jquery easyui 下载地址:http://www.jeasyui.com/download/index.php 第二步创建Java web项目 第三步导入相关的文件..文件夹结构 ...
- R语言适配问题集锦
画图时的中文乱码问题 我这是Mac Yousemite 10.10.5,在两个地方遇到了中文乱码 1.使用wordcloud包绘制中文标签云时. library(wordcloud) mydata & ...
- nginx 1.5 支持websocket
proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set ...
- 巧妇能为少米之炊(1)——Android下小内存下的生存之道
常常听到身边用安卓的朋友抱怨手机卡顿,内存动不动就快没了.而Google声称在512M的内存下也能流畅执行Android 4.4.究竟它做了什么? 总结一下它主要做了四件事: 1.优化内核,使用Act ...
- Why functions - Not only for python
It may not be clear why it is worth the trouble to divide a program into functions. There are a lot ...
- 安卓开发--AsyncTask2
package com.cnn.imageasyncdemo01; import android.app.Activity; import android.content.Intent; import ...
- Java类和对象11
首先,编写一个类ChongZai,该类中有3个重载的方法void print():其次,再编写一个主类来测试ChongZai类的功能. public class ChongZai { public v ...