我正在使用Python酸洗来序列化数据并写入流。 (&然后从流中读取 代码如下所示:
import pickle
import io
import pprint
class SimpleObject:
def __init__(self, name):
self.name = name
self.name_backwards = name[::-1]
data = []
data.append(SimpleObject('pickle'))
data.append(SimpleObject('preserve'))
data.append(SimpleObject('last'))
print (type(data))
# simulate a file
out_s = io.BytesIO
print (type(out_s))
for o in data:
print('WRITING : {} ({})'.format(o.name, o.name_backwards))
# writing to the stream
print (type(o))
pickle.dump(o, out_s)
out_s.flush()
以书面形式流式传输时出现错误-
pickle.dump(o, out_s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 704, in save_inst
write(MARK)
TypeError: descriptor 'write' requires a '_io.BytesIO' object but received a 'str'
当我打印type(o)时-将其打印为(SimpleObject的)实例
WRITING : pickle (elkcip)
<type 'instance'>
那么,为什么我会收到此错误以及如何解决?