0

i am trying to change my notebook.ipynb with code. For this i want to replace certain strings in the notebook by other strings. how do i go about this?

i used the nbformat library to read and write, and inbetween i want to change/overwrite the code. below code generates a copy of a jupyter notebook

import nbformat as nbf
with open('generictemplate.ipynb', 'r', encoding="utf-8-sig") as g:
        nb=nbf.read(g, as_version=4)
##i want to replace a string from the NB here- i need your help here
with open('generictemplate_test.ipynb', 'w') as f:

    nbf.write(nb,f)
    pass
Danny
  • 41
  • 5
  • 1
    nbformat uses the model of the notebook as cells, so you'd iterate over the cells and use [Python to do a string replace](https://realpython.com/replace-string-python/). See the bottom of the code posted [here](https://discourse.jupyter.org/t/apply-sentence-case-in-jupyter-notebook-markdown-cells/7243/2?u=fomightez) where someone wanted to capitalize the letter `i`, among other things. If your situation is simpler, you can directly use `sed` or you favorite text editor to do the find and replace directly on the json text in which the notebook is stored. – Wayne Nov 16 '22 at 19:49

0 Answers0