python3中bytes与string的互相转换
首先来设置一个原始的字符串,
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> website = 'http://www.cnblogs.com/txw1958/'
>>> type(website)
<class 'str'>
>>> website
'http://www.cnblogs.com/txw1958/'
>>>
按utf-8的方式编码,转成bytes
>>> website_bytes_utf8 = website.encode(encoding="utf-8")
>>> type(website_bytes_utf8)
<class 'bytes'>
>>> website_bytes_utf8
b'http://www.cnblogs.com/txw1958/'
>>>
按gb2312的方式编码,转成bytes
>>> website_bytes_gb2312 = website.encode(encoding="gb2312")
>>> type(website_bytes_gb2312)
<class 'bytes'>
>>> website_bytes_gb2312
b'http://www.cnblogs.com/txw1958/'
>>>
解码成string,默认不填
>>> website_string = website_bytes_utf8.decode()
>>> type(website_string)
<class 'str'>
>>> website_string
'http://www.cnblogs.com/txw1958/'
>>>
>>>
解码成string,使用gb2312的方式
>>> website_string_gb2312 = website_bytes_gb2312.decode("gb2312")
>>> type(website_string_gb2312)
<class 'str'>
>>> website_string_gb2312
'http://www.cnblogs.com/txw1958/'
>>>
python3中bytes与string的互相转换的更多相关文章
- Python3中bytes和HexStr之间的转换
1 Python3中bytes和HexStr之间的转换 ByteToHex的转换 def ByteToHex( bins ): """ Convert a byte st ...
- 【转】python3中bytes和string之间的互相转换
问题: 比对算法测试脚本在python2.7上跑的没问题,在python3上报错,将base64转码之后的串打印出来发现,2.7版本和3是不一样的:2.7就是字符串类型的,但是3是bytes类型的,形 ...
- java中Integer 和String 之间的转换
java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...
- Python3 中bytes数据类型深入理解(ASCII码对照表)
bytes的来源 bytes 是 Python 3.x 新增的类型,在 Python 2.x 中是不存在的. bytes 的意思是"字节",以字节为单位存储数据.而一个字节二进制为 ...
- C#中char[]与string之间的转换;byte[]与string之间的转化
目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...
- C#中char[]与string之间的转换
string 转换成 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string st ...
- 30-python3 中 bytes 和 string 之间的互相转换
转自:http://www.jb51.net/article/105064.htm password = b'123456' 等价于: pw = '123456' password = pw.enco ...
- VC++中 wstring和string的互相转换实现
在VC++开发中,经常会用到string和wstring,这就需要二者之间的转换,项目中封装了wstring和string相互转换的2个函数,实现如下: //将wstring转换成string std ...
- c++ 中double与string之间的转换,char *
运行代码为 /* * main.cpp * * Created on: Apr 7, 2016 * Author: lizhen */ #include <iostream> //#inc ...
随机推荐
- POJ 1821 单调队列+dp
题目大意:有K个工人,有n个墙,现在要给墙涂色.然后每个工人坐在Si上,他能刷的最大范围是Li,且必须是一个连续子区间,而且必须过Si,他刷完后能获得Pi钱 思路:定义dp[i][j]表示前i个人,涂 ...
- android studio没有浮现函数用法和属性说明?
最近转用android studio,在使用eclipse和android studio时原本在鼠标停留处或智能提示能浮现文档相关内容,但我的是一直显示Fetching Documentation…… ...
- C#入门经典(3-窗体应用程序-第二章要点)
新建一个窗体用用程序,拖一个按钮,加事件和Text属性.打开Form1Designer.cs.
- android UI中添加一张图片如何将这张图片中某一部分设为透明的
可以利用canvas画布类,这个类的具体方法可以参看官方api.http://developer.android.com/reference/android/graphics/Canvas.html ...
- JAVA实现二进制,十六进制输出
public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-gener ...
- oracle 日期相减
oracle日期相减2012-02-10 12:18--MONTHS_BETWEEN(date2,date1) 给出date2-date1的月份 SQL> select months_betwe ...
- LINQ to SQL语句对应SQL的实现
LINQ to SQL语句(1)之Where LINQ to SQL语句(2)之Select/Distinct LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg LINQ ...
- HDU 2121 Ice_cream’s world II 最小树形图 模板
开始学习最小树形图,模板题. Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32 ...
- iOS查错机制
转自: http://mp.weixin.qq.com/s?__biz=MjM5OTM0MzIwMQ==&mid=404478233&idx=2&sn=ae55d4f70fce ...
- SSH使用TCP Wrappers实现访问控制
SSH使用TCP Wrappers实现访问控制主要配置文件/etc/hosts.allow/etc/hosts.deny===TCP Wrappers的访问控制原则首先检查 hosts.allow 文 ...