How to convert .ipynb into .py files
Nov 2, 2020
import json
import osfile_name = input("Enter the path of '.ipynb' file:")
save_file_name = file_name.split("\\")[-1].split(".")[0]print(save_file_name)# Opening JSON file
f = open(file_name)
# returns JSON object as
# a dictionary
data = json.load(f)total_cells = len(data['cells'])code = []
# Iterating through the json
# list
for cell in data['cells']:
if cell['cell_type'] == "code":
for source in cell["source"]:
if not source.endswith("\n"):
source=source+"\n"
#print(source)
code.append(source)# Closing file
f.close()
'''
for c in code:
if not c.endswith("\n"):
print(c)'''pyfile = open(f"{save_file_name}.txt","w+")
for c in code:
pyfile.write(c)
pyfile.close()os.rename(f"{save_file_name}.txt",f"{save_file_name}.py")
print("Done Conversion")