pickle

Load pickle file(comes from python3) in python2

旧巷老猫 提交于 2020-02-03 03:07:24
问题 I have a pickle file, with >>> with open("wikilinks.pickle", "rb") as f: ... titles, links = pickle.load(f) ... >>> len(titles) 13421 I can load it in python3. However, when I try to load it in python2, I get this message: Traceback (most recent call last): File "<stdin>", line 2, in <module> File "/usr/lib/python2.7/pickle.py", line 1378, in load return Unpickler(file).load() File "/usr/lib/python2.7/pickle.py", line 858, in load dispatch[key](self) File "/usr/lib/python2.7/pickle.py", line

Pickle linked objects

别来无恙 提交于 2020-01-29 04:44:09
问题 I want to pickle an object and a second object that references the first. When I naively pickle/unpickle the two objects, the reference becomes a copy. How do I preserve the link between the two objects foo and bar.foo_ref ? import pickle class Foo(object): pass foo = Foo() bar = Foo() bar.foo_ref = foo with open('tmp.pkl', 'wb') as f: pickle.dump(foo, f) pickle.dump(bar, f) with open('tmp.pkl', 'rb') as f: foo2 = pickle.load(f) bar2 = pickle.load(f) print id(foo) == id(bar.foo_ref) # True

Updating Python Pickle Object

你说的曾经没有我的故事 提交于 2020-01-24 15:47:06
问题 I am doing a project in Machine Learning and for that I am using the pickle module of Python. Basically, I am parsing through a huge data set which is not possible in one execution that is why I need to save the classifier object and update it in the next execution. So my question is, when I run the program again with the new data set then will the already created pickle object be modified (or updated). If not then how can I update the same pickle object every time I run the program. save

How to read pickle that was dupmed in either python 2.7 or 3, in python 3?

纵然是瞬间 提交于 2020-01-24 12:52:27
问题 I have read that I can read pickles that were dumped in python 2.7 in python 3 using content = pickle.load(o, encoding='latin1') Obviously, I can read pickles that were dumped in python 3 using content = pickle.load(o) My problem is, I can't know the source of my pickle. It could be either one. How can I test which type of pickle I am trying to read in order to use the correct method? 回答1: inspired by @DeepSpace: def load(path): print(f"loading pkl: {os.path.abspath(path)}") assert os.path

Is there a way to pickle a scipy.interpolate.Rbf() object?

一个人想着一个人 提交于 2020-01-24 06:02:02
问题 I'm creating a radial basis function interpolation model for a rather large dataset. The main call `scipy.interpolate.Rbf(,) takes about one minute and 14 GB of RAM. Since not every machine this is supposed to run on is capable of doing this, and since the program will run on the same dataset very often, I'd like to pickle the results to a file. This is a simplified example: import scipy.interpolate as inter import numpy as np import cPickle x = np.array([[1,2,3],[3,4,5],[7,8,9],[1,5,9]]) y =

Pickle dump replaces current file data

谁说我不能喝 提交于 2020-01-23 07:34:12
问题 When I use pickle, it works fine and I can dump any load. The problem is if I close the program and try to dump again, it replaces the old file data with the new dumping. Here is my code: import pickle import os import time dictionary = dict() def read(): with open('test.txt', 'rb') as f: a = pickle.load(f) print(a) time.sleep(2) def dump(): chs = raw_input('name and number') n = chs.split() dictionary[n[0]] = n[1] with open('test.txt', 'wb') as f: pickle.dump(dictionary, f) Inpt = raw_input(

Pickle a django query?

雨燕双飞 提交于 2020-01-22 15:32:08
问题 Is it possible to pickle or somehow store a django query in the database? This won`t work : u = User.objects.all import cPickle pickled_query = cPickle.dumps(u) # and store the pickled_query in a db-field. Any thoughts? Updated: import cPickle class CustomData(models.Model): name = models.CharField(max_length = 30) pickled_query = models.CharField(max_length = 300) def get_custom_result(self): q = cPickle.loads(self.pickled_query) return q() >>> cd = CustomData(name="My data", pickled_query

Cannot load pickled object

依然范特西╮ 提交于 2020-01-22 13:05:28
问题 The problem I am having is when I try to load the pickled object. I have tried using both pickle.loads and pickle.load Here are the results: pickle.loads : TypeError: 'str' does not support the buffer interface pickle.load : TypeError: file must have 'read' and 'readline' attributes Can someone please tell me what I am doing wrong in this process? elif str(parser) == 'SwissWithdrawn_Parser': # swissprot name changes print('Gathering SwissProt update info...') cache_hits = 0 cache_misses = 0

cPickle ImportError: No module named multiarray

余生颓废 提交于 2020-01-22 12:59:25
问题 I'm using cPickle to save my Database into file. The code looks like that: def Save_DataBase(): import cPickle from scipy import * from numpy import * a=Results.VersionName #filename='D:/results/'+a[a.find('/')+1:-a.find('/')-2]+Results.AssType[:3]+str(random.randint(0,100))+Results.Distribution+".lft" filename='D:/results/pppp.lft' plik=open(filename,'w') DataOutput=[[[DataBase.Arrays.Nodes,DataBase.Arrays.Links,DataBase.Arrays.Turns,DataBase.Arrays.Connectors,DataBase.Arrays.Zones],

python pickle.dumps AssertionError

邮差的信 提交于 2020-01-21 05:19:26
问题 I'm trying to pickle a class instance containing two lists of another instances. The instances in the two lists have attributes that refer instances of each other. Here are the classes. import pickle from copy import copy class Graph: def __init__(self): self.vertices = {} self.edges = set() def __repr__(self): return "\n".join(map(str, sorted(self.vertices, key=lambda v:v.id))) class Edge: def __init__(self, vfrom, vto): self.vfrom = vfrom self.vto = vto def __hash__(self): return hash((self