2

I use Jupyter Notebook with Python. I'm not a programmer but I've been learning Python for about a year now.

I was working with some text file that I saved on the same folder of my notebooks, and I accidentally opened a .ipynb file and altered it.

As far as I can tell, I just pasted a text string. I know what I pasted, and I erased it, but now jupyter notebook can't recognize the file. Message is:

Unreadable Notebook: C:\Users\untal\Python\notas analyser.ipynb
NotJSONError('Notebook does not appear to be JSON: \'\\ufeff{\\n "cells": [\\n {\\n "cell_typ...',)

I'm not even close to be able to understand the text file to look for the problem and fix it... I don't even know if that's an option.

Is there any tool or method I can use to recover my notebook?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
José Chamorro
  • 497
  • 1
  • 6
  • 21
  • https://stackoverflow.com/questions/38819322/how-to-recover-deleted-ipython-notebooks/44044643#44044643 – Pitto Sep 25 '19 at 21:29
  • 1
    You can try opening in a text editor, like markdown or notepad and try to fix whatever mistake may still be there. Also, you may be able to open this with PyCharm, which offers support for .ipynb files (and may allow you to open this file even though jupyter won't). – H. Khan Sep 25 '19 at 21:31
  • thanks for your answer. Unfortunately, my file was not deleted but altered. I took a look on cache and I only found the damaged file... I guess that is because I tried to open it after I altered it... – José Chamorro Sep 25 '19 at 21:34
  • I'll try the second suggestion! Thanks! – José Chamorro Sep 25 '19 at 21:35

3 Answers3

3

A possible way to recover corrupted Jupyter notebook files, whether it contains text or not (size = 0KB), is to go to the project folder and display the hidden files.
Once the hidden files are displayed if you are lucky you will see a folder named '.ipynb_checkpoints'.
Open this folder and you should find your notebook.

Pitto
  • 8,229
  • 3
  • 42
  • 51
  • I tried that one, but the checkpoint is just too old. I tried to open the file with pycharm and it worked. I still have to figure out how to read the code, as I'm not used to pycharm... but it is certainly more comprehensible now! – José Chamorro Sep 25 '19 at 21:48
  • 1
    Using Pycharm worked for me. I wasn't able to actually fix the file, so I had to copy each of the original file's cells to a functional file that I created in python and then opened with Pycharm... pretty sure is not an optimal solution, but I could save all my work, so it was an effective solution! – José Chamorro Sep 26 '19 at 06:36
0

Using Pycharm worked for me. I wasn't able to actually fix the file, so I had to copy one by one each of the original file's cells to a functional file that I created in python and then opened with Pycharm... After each cell copied, I opened the file with Jupyter to check and fix any problems (going back to Pycharm). Pretty sure is not an optimal solution, but I could save all of my work, so it was an effective solution that can be used by begginers!

José Chamorro
  • 497
  • 1
  • 6
  • 21
0

The .ipynb file is a JSON file you can try to correct its syntax in online JSON editors. There are many if you look on google. (for example: https://jsoneditoronline.org/#)

Once you have a working JSON file run this text on another notebook to print the cells code.

import json

with open('./1-day.txt', 'r') as f:
    data = json.load(f)

for cell in data['cells']:
    if 'source' in cell:
        [print(i, end='') for i in cell['source'] ]
        print('\n#')

Tomas G.
  • 3,784
  • 25
  • 28