简体   繁体   中英

How do I fix a .ipynb file?

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?

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.

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!

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#')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM