Python字符串encode() (Python String encode())

Python string encode() function is used to encode the string using the provided encoding. This function returns the bytes object. If we don’t provide encoding, “utf-8” encoding is used as default.

Python字符串encoding()函数用于使用提供的编码对字符串进行编码。 此函数返回bytes对象。 如果我们不提供编码,则默认使用“ utf-8”编码。

Python字节的decode() (Python Bytes decode())

Python bytes decode() function is used to convert bytes to string object.

Python字节bytes()函数用于将字节转换为字符串对象。

Both these functions allow us to specify the error handling scheme to use for encoding/decoding errors. The default is ‘strict’ meaning that encoding errors raise a UnicodeEncodeError. Some other possible values are ‘ignore’, ‘replace’ and ‘xmlcharrefreplace’.

这两个功能使我们能够指定用于编码/解码错误的错误处理方案。 默认值为“严格”,这意味着编码错误会引发UnicodeEncodeError。 其他一些可能的值是'ignore','replace'和'xmlcharrefreplace'。

Let’s look at a simple example of python string encode() decode() functions.

我们来看一个简单的python字符串encode()encode()函数示例。

str_original = 'Hello'bytes_encoded = str_original.encode(encoding='utf-8')
print(type(bytes_encoded))str_decoded = bytes_encoded.decode()
print(type(str_decoded))print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)

Output:

输出:

<class 'bytes'>
<class 'str'>
Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True

Above example doesn’t clearly demonstrate the use of encoding. Let’s look at another example where we will get inputs from the user and then encode it. We will have some special characters in the input string entered by the user.

上面的示例没有清楚地说明编码的使用。 让我们看另一个示例,在该示例中,我们将从用户那里获取输入,然后对其进行编码。 用户输入的输入字符串中将包含一些特殊字符。

str_original = input('Please enter string data:\n')bytes_encoded = str_original.encode()str_decoded = bytes_encoded.decode()print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)

Output:

输出:

Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True
GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: str.encode() API Doc, bytes.decode() API Doc

参考: str.encode()API文档 , bytes.decode()API文档

翻译自: https://www.journaldev.com/23617/python-string-encode-decode

python字符串encode()decode()相关推荐

  1. 学Python必看,Python中encode(),decode()的本质

    学过编码解码的同学应该都知道,是二进制到我们认识的字母数字的过程叫做编码,但在python里,为什么是我们认识的字母数字到二进制叫编码呢? 我看了网上现有的讲解,都没能从本质讲清这个区别,其实要想理解 ...

  2. python3 gb2312转utf8_字符编码和python使用encode,decode转换utf-8, gbk, gb2312

    python3 爬取网页报错:'gb2312' codec can't decode byte 0xb5 in position 154969: illegal multibyte sequence ...

  3. Python字符串的encode与decode研究心得乱码问题解决方法

    Python字符串的encode与decode研究心得乱码问题解决方法 为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成"\xe4\xb8\xad\xe6\x96 ...

  4. Python 问题--encode、decode及shell中文输出

    最近在使用python的时候,涉及到中文输出的时候经常会出现乱码的问题,所以就上网搜索了下,参考了 1.Python字符串的encode与decode研究心得乱码问题解决方法: 2.python 中编 ...

  5. Python 3 字符串 encode( ) 方法

    Python 3 字符串 encode( ) 方法 Python 3.8.5 语法格式: str.encode(encoding= 'UTF-8', errors= 'strict') 描述: enc ...

  6. python中encode用法_python3中encode和decode的一些基本用法

    python3中encode和decode跟python2还是有一定的区别的,在python3中: encode(编码):按照某种规则将"文本"转换为"字节流" ...

  7. python中字符串类型的encode()方法_第五章 Python字符串常用方法详解

    5.1 Python字符串拼接(包含字符串拼接数字) 在 Python中拼接(连接)字符串很简单,可以直接将两个字符串紧挨着写在一起,具体格式为: strname = "str1" ...

  8. python 字符串内容

    一.介绍python 2和python 3中的字符串类型及用途 python 2 str 简单文本(如ascii编码)和二进制数据(bytes). unicode 多字节文本(宽字符,如中文). py ...

  9. python字符串无效的原因_python字符串问题

    相关知识点: 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unico ...

最新文章

  1. phpstudy一个域名配置两个网站(一个是thinkphp5,一个是原生php)
  2. MySQL学习总结(三)索引
  3. element-ui中el-tree树形控件-树节点的选择(选中当前节点,获取当前id并且获取其父级id)...
  4. Java之 javaObject与jsonString的转换
  5. Kubernetes — Pod
  6. 机器学习(MACHINE LEARNING)MATLAB三层神经网络的简单应用
  7. 关于 屏幕阅读器 和 sr-only
  8. 【数据竞赛】“达观杯”文本智能处理挑战赛5
  9. DTStructure分治法与最大子列和问题
  10. 使用绝对定位时浏览器大小改变排版会乱_HTML amp; CSS页面布局之定位
  11. aide入侵检测工具与crontab
  12. 怎么算掌握了mysql_mysql 需要掌握的重点
  13. IOS UI 第一篇:基本UI
  14. GDI+中发生一般性错误的解决办法(转)
  15. Oracle 对 JDK收费和.NET Core 给我们的机遇
  16. APICloud手机端网站直接制作手机APP
  17. 想知道表格文字识别怎么弄?分享2个表格识别的方法
  18. PaperWork php源码,一个开源记笔记/存档的程序:Paperwork
  19. 批量转换labelme标记为掩膜图片
  20. 程序员如何保持学习成长?

热门文章

  1. 进程/线程同步的方式和机制,进程间通信
  2. Opera 设置微软雅黑字体显示!
  3. [转载] Python中endswith() 函数法用于判断字符串是否以指定后缀结尾
  4. [转载] Python中pandas dataframe删除一行或一列:drop函数
  5. Hystrix入门与分析(一):初识Hystrix
  6. [WPF自定义控件库]自定义Expander
  7. Jrebel激活方法
  8. 网络篇-NSURLSession介绍
  9. require.js 的简单运用 --兰
  10. WEB标准 基础(一) 到底是什么?