Python3中bytes与string的互相转换
2016-10-07 22:48
671 查看
来源:python3中bytes与string的互相转换
首先来设置一个原始的字符串,
按utf-8的方式编码,转成bytes
按gb2312的方式编码,转成bytes
解码成string,默认不填
解码成string,使用gb2312的方式
首先来设置一个原始的字符串,
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与string的互相转换
- python3中bytes和string之间的互相转换
- Python笔记——python3中bytes和string之间的互相转换
- python3中bytes与string的互相转换
- python3中bytes和string之间的互相转换
- python3中bytes与string的互相转换
- python3中bytes和string之间的互相转换
- python3中bytes与string的互相转换
- python3中bytes与string的互相转换
- python 列表 字典 string 互相转换
- python list及string 互相转换
- python3中bytes与string的互相转换
- Python基础-int和string互相转换
- CString和string互相转换
- C++实现CString和string的互相转换
- java String 和int互相转换
- int 和 String 互相转换的多种方法
- date和string的互相转换
- String和Date类型互相转换