python3中字符串和字节之间转换
来源:互联网 发布:怎么样开淘宝网店 编辑:程序博客网 时间:2024/06/04 19:08
Python3中内置类型bytes和str用法及byte和string之间各种编码转换
Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。Python 3不会以任意隐式的方式混用str和bytes,正是这使得两者的区分特别清晰。你不能拼接字符串和字节包,也无法在字节包里搜索字符串(反之亦然),也不能将字符串传入参数为字节包的函数(反之亦然).
python3.0中怎么创建bytes型数据
bytes([
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
])
bytes(
"python"
,
'ascii'
)
# 字符串,编码
设置一个原始的字符串
>>> website
=
'http://www.169it.com/os'
>>>
type
(website)
<
class
'str'
>
>>> website
'http://www.169it.com/os'
>>>
按utf-8的方式编码,转成bytes
>>> website_bytes_utf8
=
website.encode(encoding
=
"utf-8"
)
>>>
type
(website_bytes_utf8)
<
class
'bytes'
>
>>> website_bytes_utf8
b
'http://www.169it.com/os'
>>>
按gb2312的方式编码,转成bytes
>>> website_bytes_gb2312
=
website.encode(encoding
=
"gb2312"
)
>>>
type
(website_bytes_gb2312)
<
class
'bytes'
>
>>> website_bytes_gb2312
b
'http://www.169it.com/os'
>>>
解码成string,默认不填
>>> website_string
=
website_bytes_utf8.decode()
>>>
type
(website_string)
<
class
'str'
>
>>> website_string
'http://www.169it.com/os'
>>>
>>>
解码成string,使用gb2312的方式
>>> website_string_gb2312
=
website_bytes_gb2312.decode(
"gb2312"
)
>>>
type
(website_string_gb2312)
<
class
'str'
>
>>> website_string_gb2312
'http://www.169it.com/os'
>>>
阅读全文
1 0
- python3中字符串和字节之间转换
- python3 字符串和字节之间的转换
- 字符串和字节数组之间的转换
- python3中字符串与字节流的转换
- C# 中字符串string和字节数组byte[]之间的转换
- android中对象与字节数组、字符串之间的转换
- 字节数组和十六进制字符串之间的转换
- java字符串和字节数组之间的转换
- 字符串对象和字节化字符数据之间的转换
- Python3中bytes和HexStr之间的转换
- Python3中byte和string之间各种编码转换
- Python3中bytes和HexStr之间的转换
- python3中bytes和string之间的互相转换
- Python3中byte和string之间各种编码转换
- Python3中bytes和HexStr之间的转换
- Java中字节流和字符流之间的转换
- 字符串,字节数组,流之间的转换
- 字符串与字节之间的转换
- WebGL之旅(二十) 帧缓冲区对象FBO
- Burp Suite详细基本用法(一):Proxy、Target模块
- python中的面向对象
- Android启动模式
- 如何理解Netfilter中的连接跟踪机制
- python3中字符串和字节之间转换
- 全面解析Java注解--慕课网笔记
- find用法
- hadoop之从数据流向角度分析MapReduce流程
- PAT基础题 4-2 多项式求值
- JMockit实践--(3)Expectations
- iOS Swift let和var的区别
- 【DP】Codeforces837D. Round Subset
- 三分学习 hdu 4454