[英]Python error: TypeError: a bytes-like object is required, not 'str'
i'm working to a personal project and i'm confronting a error: TypeError: a bytes-like object is required, not 'str'
我正在处理个人项目,并且遇到错误: TypeError: a bytes-like object is required, not 'str'
Here is my code: CLICK TO SEE THE CODE 这是我的代码: 单击查看代码
I want to make this script that is trying to find into the file a input text. 我想制作一个试图在文件中查找输入文本的脚本。 Thanks! 谢谢!
TypeError
implies that there is a mismatch in the datatype required vs given data's type. TypeError
表示所需的数据类型与给定数据的类型不匹配。 The function requires input of type 'bytes' while the code inputs data of type 'str'. 该函数需要输入“字节”类型,而代码输入“ str”类型的数据。
To convert the input string into byte-like object use str.encode
function. 要将输入字符串转换为类似字节的对象,请使用str.encode
函数。
>>> string = "abcdef"
>>> type(string)
<class 'str'>
>>> string = string.encode('ascii')
>>> type(string)
<class 'bytes'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.