TypeError: a bytes-like object is required, not 'str'

python bytes和str两种类型转换的函数encode(),decode()

  • str通过encode()方法可以编码为指定的bytes
  • 反过来,如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法

例如:

date=“12345”  #str类型

date.encode()  #转换为bytes类型

TypeError: a bytes-like object is required, not 'str'

原文地址:https://www.cnblogs.com/cxx1/p/8549443.html

时间: 2024-08-15 06:40:19

TypeError: a bytes-like object is required, not 'str'的相关文章

Python 3.5.2 TypeError: a bytes-like object is required, not 'str’问题解决方案

运行环境Mac  Python 3.5.2 Q: http_response = """\ HTTP/1.1 200 OK Hello, World! """ client_connection.sendall(http_response) TypeError: a bytes-like object is required, not 'str' 类型错误,需要的是一个byte类型,不是str类型 A: http_response = "

Socket send() method throws TypeError: a bytes-like object is required, not 'str'

python3 socket编程,发送data数据,会遇到需要bytes类型,而不是str字符串的错误 例如: import socket mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = socket.gethostbyname("www.google.com") mysock.connect(host, 80) message = "GET / HTTP/1.1\r\n\r\n"

【爬坑】Python 3.6 在 Socket 编程时出现类型错误 TypeError: a bytes-like object is required, not 'str'

1. 问题描述 Python 3.6 在 Socket 编程时出现错误如下 Traceback (most recent call last): File "F:/share/IdeaProjects/test/mypython/test/test10_tcpclient.py", line 17, in <module> sock.send(str) TypeError: a bytes-like object is required, not 'str' Process

Python Socket TypeError: a bytes-like object is required, not &#39;str&#39; 错误提示

在学习socket编程时,遇到代码返回如下错误: TypeError: a bytes-like object is required, not 'str' 发现这里python3.5和Python2.7在套接字返回值解码上有区别. 首先可以明确,在python3中默认的编码方式是unicode.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节),因此 utf-16就是现在最常用的unicode版本. 不过考虑到utf8省空间,在文件里存的

python问题:TypeError: a bytes-like object is required, not &#39;str&#39;

源程序: import socket target_host = "www.baidu.com" # 127.0.0.1 target_port = 80 # 建立一个socket对象 client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 创建TCP连接 # 连接客户端 client.connect((target_host,target_port)) client.send("GET / HTTP/1.1\r

python 3.5: TypeError: a bytes-like object is required, not &#39;str&#39;

出现该错误往往是通过open()函数打开文本文件时,使用了'rb'属性,如:fileHandle=open(filename,'rb'),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了str()函数,就会出现该错误,该错误不会再python2中出现. 具体解决方法有以下两种: 第一种,在open()函数中使用'r'属性,即文本方式读取,而不是'rb',以二进制文件方式读取,可以直接解决问题. 第二种,在open()函数中使用'rb',可以在使用之前进行转换,有以下实例,来自:htt

解决python mysql插入数据时报错:TypeError: %d format: a number is required, not str

今天在使用python爬取数据并将其写入mysql数据库时,使用该如下语句: cursor.execute( "insert into comments_p_spider(owner_id,from_name,content,create_time,score,comment_level) values(%d,%s,%s,%s,%f,%s)", (p_id,str(username), str(contentStr), str(create_time),float(score), st

atitit.解决SyntaxError: missing ] after element list&quot;不个object 挡成个str eval ....

atitit.解决SyntaxError: missing ] after element list"不个object  挡成个str eval .... 1. 原因::: 不个object  挡成个str eval ..... 1 2. eval(string) 1 2.1.1. 返回值 1 2.1.2. 说明 2 3. eval和JSON 为什么要加括号? 2 4. JSON格式的名字部分为什么要加引号? 3 5. Json的历史 3 6. 参考 3 这个只是ff错误,ie不错误.. 1. 

python TypeError: &amp;#39;builtin_function_or_method&amp;#39; object is not iterable keys

statinfo = os.stat( OneFilePath ) if AllFiles.has_key( statinfo.st_size ): OneKey = AllFiles[ statinfo.st_size ] OneKey.append( OneFilePath ) AllFiles[ statinfo.st_size ] = OneKey else: if statinfo.st_size > MinSize: # print statinfo.st_size AllFiles