Skip to content

Instantly share code, notes, and snippets.

@mcitron
Last active April 30, 2020 14:43
Show Gist options
  • Save mcitron/445a2dc921fb23ecdff0 to your computer and use it in GitHub Desktop.
Save mcitron/445a2dc921fb23ecdff0 to your computer and use it in GitHub Desktop.
Viewer for arbitrary pickle files
#!/usr/bin/python
import pickle
from collections import defaultdict
from collections import OrderedDict
import sys
def checkDict(inputData):
return (type(inputData) == dict or type(inputData) == defaultdict\
or type(inputData) == OrderedDict)
def checkList(inputData):
return type(inputData) == list
def recursivePrint(inputData,extraTuple = ()):
if checkDict(inputData):
for extraInfo,data in inputData.items():
recursivePrint(data,(extraInfo,)+extraTuple)
elif checkList(inputData):
if all([not(checkList(x) or checkDict(x)) for x in inputData]):
print (" ".join([str(x) for x in extraTuple][::-1]),inputData)
else:
for data in inputData:
recursivePrint(data,extraTuple)
else:
print (" ".join([str(x) for x in extraTuple][::-1]),inputData)
def pickleViewer(inputFile):
inputData = pickle.load(open(inputFile,'rb'))
print ("Viewing pickle file {0} which contains data of type {1}".format(inputFile,type(inputData)))
recursivePrint(inputData)
if __name__ =="__main__":
assert len(sys.argv) == 2, "Usage: pickleViewer <input.pkl>"
pickleViewer(sys.argv[1])
@valeriyvan
Copy link

I think you have lost # at the beginning of the file

@rrmehdi
Copy link

rrmehdi commented Feb 12, 2018

I guess, you'd also need to add "b" in line 17
inputData = pickle.load(open(inputFile,'rb'))

@ZenKa24
Copy link

ZenKa24 commented Oct 3, 2018

I got error below when I run your script. Please check it to me?
AssertionError: Usage: pickleViewer <input.pkl>

@Aechee
Copy link

Aechee commented Feb 7, 2019

ZanKa24, Remove the line

assert len(sys.argv) == 2, "Usage: pickleViewer <input.pkl>"

if you running it from the IDLE directly.

@ch-hristov
Copy link

I developed this https://pickleviewer.com/ for this reason :)

@mcitron
Copy link
Author

mcitron commented Apr 30, 2020

I honestly just dumped this here for myself but it's now updated to fix bugs and work in Py3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment