mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-23 20:11:10 +00:00
create script.py
This commit is contained in:
parent
72431280e8
commit
8745afc08b
20
scripts/pickle_converter/script.py
Normal file
20
scripts/pickle_converter/script.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import pickle
|
||||||
|
import pprint
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def convert_pickle(pickle_file="sample.pickle", target_file='txt'):
|
||||||
|
obj = pickle.load(open(pickle_file, "rb"))
|
||||||
|
|
||||||
|
if target_file == 'txt':
|
||||||
|
with open("out.txt", "a") as f:
|
||||||
|
pprint.pprint(obj, stream=f)
|
||||||
|
|
||||||
|
elif target_file == 'json':
|
||||||
|
json_obj = json.loads(json.dumps(obj, default=str))
|
||||||
|
|
||||||
|
with open('out.json', 'w', encoding='utf-8') as outfile:
|
||||||
|
json.dump(json_obj, outfile, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("please enter a valid doc type: 'txt', 'json'")
|
Loading…
Reference in New Issue
Block a user