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

出现该错误往往是通过open()函数打开文本文件时,使用了‘rb’属性,如:fileHandle=open(filename,‘rb‘),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了str()函数,就会出现该错误,该错误不会再python2中出现。

具体解决方法有以下两种:

第一种,在open()函数中使用‘r’属性,即文本方式读取,而不是‘rb’,以二进制文件方式读取,可以直接解决问题。

第二种,在open()函数中使用‘rb’,可以在使用之前进行转换,有以下实例,来自:http://stackoverflow.com/questions/33054527/python-3-5-typeerror-a-bytes-like-object-is-required-not-str

1:查找文件时:
with open(fname, ‘rb‘) as f:
                if b‘some-pattern‘ in tmp: continue

2:使用socketl连接时:
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((‘www.py4inf.com‘, 80))
mysock.send(**b**‘GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n‘)

while True:
    data = mysock.recv(512)
    if ( len(data) < 1 ) :
        break
    print (data);

mysock.close()

3:提前进行编码:
with open(fname, ‘rb‘) as f:
    lines = [x.decode(‘utf8‘).strip() for x in f.readlines()]

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

时间: 2024-10-15 23:05:50

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

Python 3.5.2 TypeError: a bytes-like object is required, not &#39;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 = "

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

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

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

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' 原文

Socket send() method throws TypeError: a bytes-like object is required, not &#39;str&#39;

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 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

python 报错TypeError: &#39;range&#39; object does not support item assignment,解决方法

贴问题 nums = range(5)#range is a built-in function that creates a list of integers print(nums)#prints "[0,1,2,3,4]" print(nums[2:4])#Get a slice from index 2 to 4 (exclusive); prints '[2,3]" print(nums[2:])#Get a slice from index 2 to the end

Python错误:TypeError:&#39;str&#39; does not support the buffer interface

在socket套接字模块进行send和recv方法时出现这种问题,是因为Python3.x和Python2.x版本变化,In python 3, bytes strings and unicodestrings are now two different types. 相互之间需要进行转换decode()和encode(). send()需要的参数为bytes类型,因此需要对str进行encode() recv()返回的是bytes类型,因此我们需要对返回的bytes进行decode()转换为s