Is there a way to view cPickle or Pickle file contents without loading Python in Windows?
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.
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.
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.
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
The answer by nth is often all you need:
python -m pickle data.pickle
@mgilson makes the point that if the pickle contains user-defined data types, that method will fail, e.g. with
AttributeError: Can't get attribute 'Foo' on <module 'pickle' from '/usr/lib/python3.10/pickle.py'>
In that case, you can still get at least some information via pickletools, which is also safer since it doesn't execute any pickle bytecode:
$ python -m pickletools data.pickle
0: \x80 PROTO 4
2: \x95 FRAME 23
11: \x8c SHORT_BINUNICODE '__main__'
21: \x94 MEMOIZE (as 0)
22: \x8c SHORT_BINUNICODE 'Foo'
27: \x94 MEMOIZE (as 1)
28: \x93 STACK_GLOBAL
29: \x94 MEMOIZE (as 2)
30: ) EMPTY_TUPLE
31: \x81 NEWOBJ
32: \x94 MEMOIZE (as 3)
33: . STOP
highest protocol among opcodes = 4
Adding the -a
option explains the bytecodes. I dare say that there are some clever schemes out there to make that prettier and more helpful, but it is a start.
'개발하자' 카테고리의 다른 글
주피터 노트북에 원시 파이썬 파일 생성 (0) | 2023.06.13 |
---|---|
모든 장치에서 컨테이너 자동 확장 (0) | 2023.06.13 |
Python에서의 슬라이싱 작동 방식 (1) | 2023.06.12 |
파이썬 요청 모듈을 사용하는 것을 제외하고 시도하는 올바른 방법은 무엇입니까? (0) | 2023.06.11 |
다음으로 python 2.5 가져오기 (1) | 2023.06.11 |