Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to view cPickle or Pickle file contents without loading Python in Windows?

Tags:

python

pickle

I use cPickle to save data sets from each run of a program. Since I sometimes need to see the outline of the data without running the code, I would like an easy way to quickly view the contents by just double-clicking on the file. I am trying to avoid having to load a terminal and pointing python to a file each time, just to run some print script.

I looked for Notepad++ plugins but couldn't find anything.

Is there some easy way to do this? Does anyone have any suggestions?

Note: I run Windows 7.

like image 813
TimY Avatar asked Nov 26 '12 16:11

TimY


People also ask

How do I view the content of a pickle file?

The pandas module has a read_pickle() method that can be used to read a pickle file. This method accepts a filepath_or_buffer argument: the file path, the URL, or the buffer from where the pickle file will be loaded. This function will return an unpickled object of the file.

How do I open a PKL file in Windows?

If you cannot open your PKL file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a PKL file directly in the browser: Just drag the file onto this browser window and drop it.

How do I open a pickle dataset?

Pickling Files To use pickle, start by importing it in Python. To pickle this dictionary, you first need to specify the name of the file you will write it to, which is dogs in this case. Note that the file does not have an extension. To open the file for writing, simply use the open() function.


2 Answers

For Python 3.2+/2.7+ you can view (__repr__'s of) pickles from the command-line:

$ python -c "import pickle; pickle.dump({'hello': 'world'}, open('obj.dat', 'wb'))" $ python -mpickle obj.dat {'hello': 'world'} 

It should be easy to integrate this into the Windows shell.

like image 103
nth Avatar answered Oct 25 '22 00:10

nth


I REALLY doubt that there's any way to do this since with pickle, you can pack in pretty much anything. When unpickling, you need to be able to load the modules etc. that were loaded when the object was pickled. In other words, in general, to be able to unpickle something, python needs to be able to reproduce the "environment" of the program (or at least a close enough approximation) -- loaded modules, classes in the global namespace, etc ... In general, this isn't possible without some help from the user. Consider:

import pickle class Foo(object): pass  a = Foo() with open('data.pickle','wb') as f:     pickle.dump(a,f) 

Now if you try to restore this in a separate script, python has no way of knowing what a Foo looks like and so it can't restore the object (unless you define an suitable Foo object in that script). This isn't really a process that can be done without some human intervention.

Of course, an arguably useful special case where you're just pickling builtin objects and things from the standard library might be able to be attempted ... but I don't think you could write a general unpickler extension.

like image 33
mgilson Avatar answered Oct 25 '22 02:10

mgilson