这个问题在这里已经有了答案:
Pickle.dump to variable
(3 个回答)
3年前关闭。
我已经看到并编写了用 Python pickle 对象的代码。但它们都创建物理文件来包含数据。我希望我将数据写入内存并读取并 pickle 并传输它。
甚至有可能吗?
from PIL import ImageGrab
import io
import codecs
import pickle
# Model class to send Process Data to the server
class ProcessData:
process_id = 0
project_id = 0
task_id = 0
start_time = 0
end_time = 0
user_id = 0
weekend_id = 0
# Model class to send image data to the server
class ProcessScreen:
process_id = 0
image_data = bytearray()
image_name = "Dharmindar_screen.jpg"
ImageGrab.grab().save(image_name,"JPEG")
image_data = None
with codecs.open(image_name,'rb') as image_file:
image_data = image_file.read()
serialized_process_data = io.BytesIO()
process_data = ProcessData()
process_data.process_id = 1
process_data.project_id = 2
process_data.task_id = 3
process_data.user_id = 4
process_data.weekend_id = 5
process_data.start_time = 676876
process_data.end_time = 787987
process_screen = ProcessScreen()
process_screen.process_id = process_data.process_id
process_screen.image_data = image_data
prepared_process_data = (process_data, process_screen)
process_data_serializer = pickle.Pickler()
process_data_serializer(serialized_process_data).dump(prepared_process_data)
print('Data serialized.')
if process_data_serializer is not None:
d = process_data_serializer.getvalue()
deserialized_data = None
with open(d, 'rb') as serialized_data_file:
process_deserializer = pickle.Unpickler(serialized_data_file)
deserialized_data = process_deserializer.load()
else:
print('Empty')
上面的代码抛出 TypeError: Required argument 'file' (pos 1) not found
最佳答案
您传递给 pickle.dump
的 File 对象只需要一个写方法,见https://docs.python.org/2/library/pickle.html#pickle.dump
file must have a write() method that accepts a single string argument. It can thus be a file object opened for writing, a StringIO object, or any other custom object that meets this interface.
例如,您甚至可以创建自己的类来存储 pickle 数据,而不是使用 StringIO 对象
class MyFile(object):
def __init__(self):
self.data = []
def write(self, stuff):
self.data.append(stuff)
然后只是pickle到这个类的一个实例:
class ExampleClass(object):
def __init__(self, x):
self.data = x
a = ExampleClass(123)
f = MyFile()
pickle.dump(a, f)
正如@rawing 所提议的,另一种选择是使用
pickle.dumps
这将直接返回一个您可以使用的字符串,比较 here .
关于python - 我可以在内存中 pickle Python 对象而不是物理文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47307107/