Python - bytes与字符串的相互转化

decode和encode的区别和介绍

 by.decode(encoding='UTF-8',errors='strict') 

 str.encode(encoding='UTF-8',errors='strict') 

  • 显而易见decode是解码,encode是编码
  • 解码代表bytes类型转成str类型
  • 编码代表str类型转成bytes类型
  • 而bytes类型的数据一般在写入文件时需要用到

 

直接上代码

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 """
 5 __title__  = 
 6 __Time__   = 2020/2/21 15:56
 7 
 8 """
 9 # bytes转字符串方式一
10 b = b'\xe9\x80\x86\xe7\x81\xab'
11 string = str(b, 'utf-8')
12 print(string)
13 
14 # bytes转字符串方式二
15 b = b'\xe9\x80\x86\xe7\x81\xab'
16 string = b.decode()  # 第一参数默认utf8,第二参数默认strict
17 print(string)
18 
19 # bytes转字符串方式三
20 b = b'\xe9\x80\x86\xe7\x81haha\xab'
21 string = b.decode('utf-8', 'ignore')  # 忽略非法字符,用strict会抛出异常
22 print(string)
23 
24 # bytes转字符串方式四
25 b = b'\xe9\x80\x86\xe7\x81haha\xab'
26 string = b.decode('utf-8', 'replace')  # 用?取代非法字符
27 print(string)
28 
29 # 字符串转bytes方式一
30 str1 = '逆火'
31 b = bytes(str1, encoding='utf-8')
32 print(b)
33 
34 # 字符串转bytes方式二
35 b = str1.encode('utf-8')
36 print(b)

 

执行结果

逆火
逆火
逆haha
逆�haha�
b'\xe9\x80\x86\xe7\x81\xab'
b'\xe9\x80\x86\xe7\x81\xab'

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python中,可以使用不同的方法将bytes对象转换为字符串。 方法一是使用`str()`函数,指定字符集将bytes对象转换为字符串。例如,`str(b, 'utf-8')`将bytes对象`b`转换为UTF-8编码的字符串。 方法二是使用bytes对象的`decode()`方法,不指定字符集默认为UTF-8。例如,`b.decode()`将bytes对象`b`转换为字符串。 方法三是使用`decode()`方法,并指定字符集。例如,`b.decode('utf-8', 'ignore')`将bytes对象`b`转换为UTF-8编码的字符串,并忽略非法字符。 方法四是使用`decode()`方法,并指定字符集,用问号取代非法字符。例如,`b.decode('utf-8', 'replace')`将bytes对象`b`转换为UTF-8编码的字符串,并用问号取代非法字符。 另外,如果要将字符串转换为bytes对象,可以使用两种方法。方法一是使用`bytes()`函数,指定字符集将字符串转换为bytes对象。例如,`bytes(str1, encoding='utf-8')`将字符串`str1`转换为UTF-8编码的bytes对象。方法二是使用字符串的`encode()`方法,不指定字符集默认为UTF-8。例如,`str1.encode('utf-8')`将字符串`str1`转换为UTF-8编码的bytes对象。 综上所述,根据不同的需求和情况,可以选择适合的方法将bytes对象转换为字符串或将字符串转换为bytes对象。 #### 引用[.reference_title] - *1* [Python bytes字节串与string字符串之间的转换](https://blog.csdn.net/zhiweihongyan1/article/details/122105183)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python bytes字符串相互转化](https://blog.csdn.net/qq_26535271/article/details/86495717)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【pythonbytes字符串相互转化](https://blog.csdn.net/weixin_42306440/article/details/88423449)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小菠萝测试笔记

来支持下测试小锅锅

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值