Merge pull request #98 from muratonuryildirim/master

Issue No: 88 pickle to txt or json
This commit is contained in:
Advaita Saha 2022-10-04 10:02:40 +05:30 committed by GitHub
commit e7d673430e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,6 @@
# .pickle to .txt or .json
This script converts .pickle files to .txt or .json files.
## Usage
* Dependency:
* *None*

View 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'")