python3中bytes与string的互相转换
2017-01-15 21:44
399 查看
首先来设置一个原始的字符串,
按utf-8的方式编码,转成bytes
按gb2312的方式编码,转成bytes
解码成string,默认不填
解码成string,使用gb2312的方式
1 Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32 2 Type "help", "copyright", "credits" or "license" for more information. 3 >>> website = 'http://www.cnblogs.com/txw1958/' 4 >>> type(website) 5 <class 'str'> 6 >>> website 7 'http://www.cnblogs.com/txw1958/' 8 >>>
按utf-8的方式编码,转成bytes
1 >>> website_bytes_utf8 = website.encode(encoding="utf-8") 2 >>> type(website_bytes_utf8) 3 <class 'bytes'> 4 >>> website_bytes_utf8 5 b'http://www.cnblogs.com/txw1958/' 6 >>>
按gb2312的方式编码,转成bytes
1 >>> website_bytes_gb2312 = website.encode(encoding="gb2312") 2 >>> type(website_bytes_gb2312) 3 <class 'bytes'> 4 >>> website_bytes_gb2312 5 b'http://www.cnblogs.com/txw1958/' 6 >>>
解码成string,默认不填
1 >>> website_string = website_bytes_utf8.decode() 2 >>> type(website_string) 3 <class 'str'> 4 >>> website_string 5 'http://www.cnblogs.com/txw1958/' 6 >>> 7 >>>
解码成string,使用gb2312的方式
1 >>> website_string_gb2312 = website_bytes_gb2312.decode("gb2312") 2 >>> type(website_string_gb2312) 3 <class 'str'> 4 >>> website_string_gb2312 5 'http://www.cnblogs.com/txw1958/' 6 >>>
相关文章推荐
- 利用python进行性能测试(下)
- Python之协同过滤(寻找相近的用户)
- 32 Python 多线程 - threading
- python中的paste deploy
- 31 Python 多进程-multiprocessing
- 如何使用python timeit模块使用实践
- Python Day1
- 利用Python进行性能测试(上)
- Python环境变量配置
- python 3-2 如何实现可迭代对象和迭代器对象(2) BookIterable/BookIterator
- Python中re的match、search、findall、finditer区别
- Python yield生成器的send方法考察
- Python零基础入门十四之对象
- python数据分析之(3)pandas
- Python实现快排
- Python-读写文件
- Python 实现堆排序
- [转]python中@classmethod @staticmethod区别
- keras 安装
- 总结python实现父类调用两种方法的不同