iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python3中bytes与string
  • 162
分享到

python3中bytes与string

bytesstring 2023-01-31 01:01:01 162人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

首先来设置一个原始的字符串, python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyri

首先来设置一个原始的字符串

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/'
>>>

 

--结束END--

本文标题: python3中bytes与string

本文链接: https://www.lsjlt.com/news/184717.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作