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' >
总结
以上所述是小编给大家介绍的Python3中内置类型bytes和str用法及byte和string之间各种编码转换 问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 【中国音乐】盛中国《新疆之春》2004[FLAC+CUE]
- 群星《音你而来 第8期》[320K/MP3][44.64MB]
- 群星《音你而来 第8期》[FLAC/分轨][121.21MB]
- 群星《十二女声Ⅲ》[WAV/CUE/分轨][626.4MB]
- 陈百强.1993-离不开...纪念歌集2CD【华纳】【WAV+CUE】
- 于台烟.2003-爱自己【乾坤唱片】【WAV+CUE】
- 群星.2024-我们永远是我们电影原声带【乐禾发文化】【FLAC分轨】
- 【中国民乐】张维良《秋江夜泊》2004[FLAC+CUE]
- 明达年度发烧碟MasterSuperiorAudiophile2011[DSF]
- 明达年度发烧碟MasterSuperiorAudiophile2017[DSF]
- 翁立友.2008-恨孤单【大旗】【WAV+CUE】
- 群星.2009-Magic.Cover日粤精华3CD【环球】【WAV+CUE】
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]