简体   繁体   English

Python错误:TypeError:需要一个类似字节的对象,而不是'str'

[英]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.

相关问题 Python 错误类型错误:需要类似字节的 object,而不是“str” - Python ERROR TypeError: a bytes-like object is required, not 'str' python错误TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - python error TypeError: a bytes-like object is required,not 'str' Python SocketServer 错误:TypeError:需要类似字节的对象,而不是“str” - Python SocketServer Error : TypeError: a bytes-like object is required, not 'str' 需要一个类似字节的对象,而不是&#39;str&#39;:TypeError - a bytes-like object is required, not 'str' : TypeError TypeError: bytes-like object 是必需的,而不是 'str' - TypeError: bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是“str” - TypeError: a bytes-like object is required, not 'str' TypeError:需要类似字节的 object,而不是“str”? - TypeError: a bytes-like object is required, not 'str'? TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - TypeError: a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,对于无服务器和Python3来说不是&#39;str&#39; - TypeError: a bytes-like object is required, not 'str' with serverless and Python3 Python 3,TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - Python 3, TypeError: a bytes-like object is required, not 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM