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 ...
随机推荐
- 以前的loginUI
的 <%@ page language="java" pageEncoding="UTF-8"%> <%@ include file=&quo ...
- FLASH和EEPROM的最大区别
源:http://www.cnblogs.com/bingoo/p/3551753.html FLASH和EEPROM的最大区别是FLASH按扇区操作,EEPROM则按字节操作,二者寻址方法不同,存储 ...
- zf-关于注册码过期
Webroot-index.jsp下 少写了个函数 导致登陆进去不能弹出注册码过期的对话框,函数如下 window.onload = function() { <ww:iterator valu ...
- angularJS 系列(五)--controller AS 语法
原文: http://www.cnblogs.com/whitewolf/p/3493362.html 这篇国外的文章也非常好: http://codetunnel.io/angularjs-cont ...
- HDU 2546 饭卡 01背包变形
题目大意:中文题就不多说了 题目思路:由题意可知,只要高于5元,就可以随便刷,那我们就把最贵的留在最后刷.但是如果低于5元就什么也不能刷(哪怕你要买的物品价格不足五元),所以我们可以先求出(n-5)元 ...
- 3D动画效果照片墙demo
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF- ...
- n个List<Map>合并,Map中某属性值相等的value值相加
List<Map> maps1 =[{"bigtypes":100,"num":400},{"bigtypes":200,&qu ...
- 关于源码编译每次提示有错误 要make update-api
最近编译newline的版本的时候..同事修改了andoid默认输入法为百度.这是系统自动提供的API,所以每次编译会提示 此时在编译源码生成SDK的过程中会出现这个问题:************** ...
- win10 下使用虚拟机安装ubuntu及其网络配置
通过虚拟机安装ubuntu 我的机器是64位的win10系统,使用的虚拟机VMware workstation 12 pro 安装的是ubuntu 14.04, 网上教程很多,很详细也有有效 win1 ...
- linux iptables 相关设置
首先在使用iptables之前敲入一下两条命令 > iptables -F #这句话的意思是清空所有的链 > iptables -X #这句话的意思是清空所有自定义的链 以上两条的含 ...