Har*_*pta 2 python string byte file-handling python-3.x
我正在尝试使用文件中的数据集在 ML 中实现我的项目
authors_file_handler = open(authors_file, "r")
authors = pickle.load(authors_file_handler)
authors_file_handler.close()
Run Code Online (Sandbox Code Playgroud)
在此之后,我在这一行出现错误
作者 = pickle.load(authors_file_handler)
TypeError: a bytes-like object is required, not 'str'
Run Code Online (Sandbox Code Playgroud)
您需要以二进制读取模式打开文件:
authors_file_handler = open(authors_file, "rb") # Note the added 'b'
authors = pickle.load(authors_file_handler)
authors_file_handler.close()
Run Code Online (Sandbox Code Playgroud)
从pickle.load() 文档:
参数文件必须有两个方法,一个采用整数参数的 read() 方法,以及一个不需要参数的 readline() 方法。这两种方法都应该返回字节。因此,文件可以是为二进制读取而打开的磁盘文件 、io.BytesIO 对象或任何其他符合此接口的自定义对象。
| 归档时间: |
|
| 查看次数: |
980 次 |
| 最近记录: |