Skip to content

Instantly share code, notes, and snippets.

@javabrett
Forked from valeriyvan/pickleViewer.py
Last active September 22, 2017 06:12
Show Gist options
  • Save javabrett/f8af838f721455ccd450a4ae672ff062 to your computer and use it in GitHub Desktop.
Save javabrett/f8af838f721455ccd450a4ae672ff062 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.iteritems():
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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment