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/
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.
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.
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).
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With