Python3中内置类型bytes和str用法及byte和string之间各种编码转换
Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。Python 3不会以任意隐式的方式混用str和bytes,正是这使得两者的区分特别清晰。你不能拼接字符串和字节包,也无法在字节包里搜索字符串(反之亦然),也不能将字符串传入参数为字节包的函数(反之亦然).
python3.0中怎么创建bytes型数据
|
1
2
|
bytes([1,2,3,4,5,6,7,8,9])bytes("python", 'ascii') # 字符串,编码 |
设置一个原始的字符串
|
1
2
3
4
5
6
|
>>> website = 'http://www.169it.com/os'>>> type(website)<class 'str'>>>> website'http://www.169it.com/os'>>> |
按utf-8的方式编码,转成bytes
|
1
2
3
4
5
6
|
>>> website_bytes_utf8 = website.encode(encoding="utf-8")>>> type(website_bytes_utf8)<class 'bytes'>>>> website_bytes_utf8b'http://www.169it.com/os'>>> |
按gb2312的方式编码,转成bytes
|
1
2
3
4
5
6
|
>>> website_bytes_gb2312 = website.encode(encoding="gb2312")>>> type(website_bytes_gb2312)<class 'bytes'>>>> website_bytes_gb2312b'http://www.169it.com/os'>>> |
解码成string,默认不填
|
1
2
3
4
5
6
7
|
>>> website_string = website_bytes_utf8.decode()>>> type(website_string)<class 'str'>>>> website_string'http://www.169it.com/os'>>>>>> |
解码成string,使用gb2312的方式
|
1
2
3
4
5
6
|
>>> website_string_gb2312 = website_bytes_gb2312.decode("gb2312")>>> type(website_string_gb2312)<class 'str'>>>> website_string_gb2312'http://www.169it.com/os'>>> |
Python3中内置类型bytes和str用法及byte和string之间各种编码转换的更多相关文章
- Python3中内置类型bytes和str用法及byte和string之间各种编码转换,python--列表,元组,字符串互相转换
Python3中内置类型bytes和str用法及byte和string之间各种编码转换 python--列表,元组,字符串互相转换 列表,元组和字符串python中有三个内建函数:,他们之间的互相转换 ...
- C#中char[]与string之间的转换;byte[]与string之间的转化
目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...
- JAVA中java.util.Date、java.sql.Timestamp和String之间的互相转换
java.util.Date与的String互转 java.util.Date---->String /** * 将java.util.Date对象转化为String字符串 * @param d ...
- 【转】python3中bytes和string之间的互相转换
问题: 比对算法测试脚本在python2.7上跑的没问题,在python3上报错,将base64转码之后的串打印出来发现,2.7版本和3是不一样的:2.7就是字符串类型的,但是3是bytes类型的,形 ...
- 30-python3 中 bytes 和 string 之间的互相转换
转自:http://www.jb51.net/article/105064.htm password = b'123456' 等价于: pw = '123456' password = pw.enco ...
- Python3 中 configparser 模块解析配置的用法详解
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...
- Python3中strip()、lstrip()、rstrip()用法详解
Python中有三个去除头尾字符.空白符的函数,它们依次为: strip: 用来去除头尾字符.空白符(包括\n.\r.\t.' ',即:换行.回车.制表符.空格) lstrip:用来去除开头字符.空白 ...
- Python3中IO文件操作的常见用法
首先创建一个文件操作对象: f = open(file, mode, encoding) file指定文件的路径,可以是绝对路径,也可以是相对路径 文件的常见mode: mode = “r” # ...
- Python3基础 内置函数 eval str转为list tuple dict
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
随机推荐
- Javascript Madness: Mouse Events
http://unixpapa.com/js/mouse.html Javascript Madness: Mouse Events Jan WolterAug 12, 2011 Note: I ha ...
- java 生成Excel开门篇
本随笔的Excel所用的poi jar包(3.17版本)链接: https://pan.baidu.com/s/1gaa3dJueja8IraUDYCSLIQ 提取密码: 9xr7 简单实现:两个类: ...
- 撰写html标签的快捷方式2
1.写一个含有类的标签. div.demo-inline + tap键 就会生成如下<div class="demo-inline"></div> 2.CS ...
- 基于socket的简单p2p聊天项目
https://blog.csdn.net/Jacky_Can/article/details/74984822 https://blog.csdn.net/qq_20889581/article/d ...
- Code Signal_练习题_All Longest Strings
Given an array of strings, return another array containing all of its longest strings. Example For i ...
- centos 快速安装wordpress
1.两种方式得到Wordpress 首先你可以去wordpress官方网站看下最新的wordpress的下载地址多少.比如wordpress 3.9.1的下载地址是: http://cn.wordpr ...
- MySql基本学习知识点:
1.Mysql的简介: (1):常识: MySQL是一种关系数据库管理系统,是一种开源软件 由瑞典MySQL AB公司开发,2008年1月16号被Sun公司收购.2009年,SUN又被Oracle收购 ...
- Unable to load script from assets 'index.android.bundle' 出错?
野路子太多,坑人真的!F**k 言归正传,当你运行 react native 程序的时候出现这个错误 ,如果您使用Windows,请按以下方式运行命令,或者如果出现错误“无法找到条目文件index.a ...
- 图片按钮(imageButton)
图片按钮(imageButton) 常用属性: android:src="@drawable/download" (这里的download是一张图片的名称,通过引用该图片的名称直接 ...
- windows Ctrl + Alt + 方向键 取消屏幕反转
1.在桌面右击 2.再次右击桌面 3.单击选项和支持 4.点击禁用和应用