简体   繁体   English

有没有办法在 Windows 中不加载 Python 的情况下查看 cPickle 或 Pickle 文件内容?

[英]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.我使用 cPickle 来保存程序每次运行的数据集。 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.我试图避免每次都加载终端并将 python 指向一个文件,只是为了运行一些print脚本。

I looked for Notepad++ plugins but couldn't find anything.我寻找 Notepad++ 插件但找不到任何东西。

Is there some easy way to do this?有什么简单的方法可以做到这一点吗? Does anyone have any suggestions?有没有人有什么建议?

Note: I run Windows 7.注意:我运行的是 Windows 7。

For Python 3.2+/2.7+ you can view ( __repr__ 's of) pickles from the command-line:对于 Python 3.2+/2.7+,您可以__repr__查看( __repr__的)泡菜:

$ 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.将其集成到 Windows shell 中应该很容易。

I REALLY doubt that there's any way to do this since with pickle , you can pack in pretty much anything.真的怀疑有什么办法可以做到这一点,因为使用pickle ,您几乎可以打包任何东西。 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.换句话说,一般来说,为了能够unpickle某些东西,python 需要能够重现程序的“环境”(或至少是足够接近的近似值)——加载的模块、全局命名空间中的类等。 .. 一般来说,如果没有用户的帮助,这是不可能的。 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).现在,如果您尝试在单独的脚本中恢复它,python 无法知道Foo是什么样子,因此它无法恢复对象(除非您在该脚本中定义了合适的Foo对象)。 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.当然,可以尝试一个可以说是有用的特殊情况,即您只是从标准库中腌制内置对象和事物……但我认为您无法编写通用的 unpickler 扩展。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM