Reputation: 525
I have read a Jupyter notebook file (ipynb) into Jupyter as a json object for cleaning up purposes:
import json
with open('C:/Python/Scripts/MyNotebook.ipynb') as json_file:
jsonin= json.load(json_file)
I then remove some blocks of code that don't have the specified string #keepthis
:
jsonout=jsonin
if '#keepthis' not in str(jsonout['cells'][1]['source']):
jsonout['cells'][1] = np.nan
This works fine. How do I now convert jsonout back into an .ipynb file? I have tried this:
!jupyter nbconvert --to jsonout 'C:/Python/Scripts/MyNotebookClean.ipynb'
But I get this error despite manually creating the MyNotebookClean.ipynb
file in the same location as the original MyNotebook.ipynb
file: [NbConvertApp] WARNING | pattern "'C:/Python/Scripts/MyNotebookClean.ipynb'" matched no files
Upvotes: 1
Views: 3249
Reputation: 525
I found the answer to writing the json object back to the ipynb file. It is simply:
with open('C:/Python/Scripts/MyNotebookClean.ipynb', 'w') as outfile:
json.dump(jsonout, outfile)
Also, I should have used del jsonout['cells'][1]
instead of jsonout['cells'][1] = np.nan
Upvotes: 1