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

后端 未结 3 1836
星月不相逢
星月不相逢 2020-12-29 03:40

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 quick

相关标签:
3条回答
  • 2020-12-29 03:50

    You can also make an alias on your terminal, for example :

    alias pvw="python -mpickle "
    

    in my case :

    pvw obj.dat                                   
             ID    A_ID   B_ID   PAST_ID
        0    20    1008   4771     425  
        1    20    2000   4771     425  
        2    20    2015   4771     425
    
    0 讨论(0)
  • 2020-12-29 03:57

    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.

    0 讨论(0)
  • 2020-12-29 04:09

    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.

    0 讨论(0)
提交回复
热议问题