Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to write a cPickle object but get a 'write' attribute type error

Tags:

python

pickle

When trying to apply some code I found on the internet in iPython, it's coming up with an error:

TypeError                                 Traceback (most recent call last)     <ipython-input-4-36ec95de9a5d> in <module>()      13     all[i] = r.json()      14  ---> 15 cPickle.dump(all, outfile)  TypeError: argument must have 'write' attribute 

Here's what I have done in order:

outfile = "C:\John\Footy Bants\R COMPLAEX MATHS" 

Then, I pasted in the following code:

import requests, cPickle, shutil, time  all = {} errorout = open("errors.log", "w")  for i in range(600):     playerurl = "http://fantasy.premierleague.com/web/api/elements/%s/"     r = requests.get(playerurl % i)      # skip non-existent players     if r.status_code != 200: continue      all[i] = r.json()  cPickle.dump(all, outfile) 

Here's the original article to give you an idea of what I'm trying to achieve:

http://billmill.org/fantasypl/

like image 827
Johnliquid Avatar asked Mar 18 '15 16:03

Johnliquid


People also ask

What is cPickle in Python?

This process is also called serializing ” the object. The byte stream representing the object can then be transmitted or stored, and later reconstructed to create a new object with the same characteristics. The cPickle module implements the same algorithm, in C instead of Python.

What is the difference between cPickle and pickle module?

The cPickle module helps us by implementing an algorithm for turning an arbitrary python object into a series of Bytes. The Pickle module also carries a similar type of program. But the difference between the 2 is that cPickle is much faster and implements the algorithm in C.

Why can't I use Pickle to find the class of a function?

Your problem is (in part) because you only have one file in your program. pickle is lazy and does not serialize class definitions or function definitions. Instead it saves a reference of how to find the class (the module it lives in and its name).

Can a module be pickled in the unpickling environment?

Neither the function’s code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised.


1 Answers

The second argument to cPickle.dump() must be a file object. You passed in a string containing a filename instead.

You need to use the open() function to open a file object for that filename, then pass the file object to cPickle:

with open(outfile, 'wb') as pickle_file:     cPickle.dump(all, pickle_file) 

See the Reading and Writing Files section of the Python tutorial, including why using with when opening a file is a good idea (it'll be closed for you automatically).

like image 146
Martijn Pieters Avatar answered Sep 21 '22 14:09

Martijn Pieters