-
-
Save fransua/c24c2afed4b13ec7522ee4af3db73c24 to your computer and use it in GitHub Desktop.
For converting Python 2 pickles to Python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# kimchi.py | |
# For converting Python 2 pickles to Python 3 | |
import os | |
import dill | |
import pickle | |
import argparse | |
def convert(old_pkl): | |
""" | |
Convert a Python 2 pickle to Python 3 | |
""" | |
# Make a name for the new pickle | |
new_pkl = os.path.splitext(os.path.basename(old_pkl))[0]+"_p3.pkl" | |
# Convert Python 2 "ObjectType" to Python 3 object | |
dill._dill._reverse_typemap["ObjectType"] = object | |
# Open the pickle using latin1 encoding | |
with open(old_pkl, "rb") as f: | |
loaded = pickle.load(f, encoding="latin1") | |
# Re-save as Python 3 pickle | |
with open(new_pkl, "wb") as outfile: | |
pickle.dump(loaded, outfile) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
description="Convert a Python 2 pickle to Python 3" | |
) | |
parser.add_argument("infile", help="Python 2 pickle filename") | |
args = parser.parse_args() | |
convert(args.infile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment