Python TypeError:需要类似字节的对象,而不是';str';从文件中读取时
我正试图使用文件中的数据集在ML中实现我的项目Python TypeError:需要类似字节的对象,而不是';str';从文件中读取时,python,string,python-3.x,byte,file-handling,Python,String,Python 3.x,Byte,File Handling,我正试图使用文件中的数据集在ML中实现我的项目 authors_file_handler = open(authors_file, "r") authors = pickle.load(authors_file_handler) authors_file_handler.close() 在这之后,我在这行中遇到了错误 authors=pickle.load(authors\u file\u处理程序) 您需要以二进制读取模式打开文件: authors_file_handler = open(au
authors_file_handler = open(authors_file, "r")
authors = pickle.load(authors_file_handler)
authors_file_handler.close()
在这之后,我在这行中遇到了错误
authors=pickle.load(authors\u file\u处理程序)
您需要以二进制读取模式打开文件:
authors_file_handler = open(authors_file, "rb") # Note the added 'b'
authors = pickle.load(authors_file_handler)
authors_file_handler.close()
从pickle.load()
中:
参数文件必须有两个方法,一个read()方法采用
整数参数,以及不需要参数的readline()方法。
这两种方法都应该返回字节。因此,文件可以是磁盘上的文件
打开进行二进制读取、io.BytesIO对象或任何其他自定义
满足此接口的对象
您在哪一行得到错误?请提供完整的回溯。请尝试:
authors=pickle.load(字节(authors\u file\u handler,encoding=“utf-8”)
authors_file_handler = open(authors_file, "rb") # Note the added 'b'
authors = pickle.load(authors_file_handler)
authors_file_handler.close()