简体   繁体   中英

reading ipynb file into python

 def request_sender(i):
      request to the database with api calls

 dict1 = {"file1":"id1,id2,id3",
     "file2":"id4,id5,id6"}

 for i in dict1.keys():
     for j in dict1[i]:
         request_sender(i)

currently my script automatically run when all the ids of a single file are updated and each file is an ipynb file i can import using %run the code is working perfectly.

The only problem here is i have to manually enter the file and ids the id's are inside the file also if am able to read the file into my python code and process the code using regex function or string manipulation i can find and fetch the keys.

but i don't know how to read an ipynb file into my python code

I found the answer i started working with different ways to open i went through the ipynb file type on more time i understand that it is an html file written in json format so we can use html method to read it.

 import codecs
 f=codecs.open("DTR IMEI - VAS.ipynb", 'r')
 print(f.read())

What about this:

import codecs
import json

f = codecs.open("JupFileName.ipynb", 'r')
source = f.read()

y = json.loads(source)
pySource = '##Python .py code from .jpynb:\n'
for x in y['cells']:
     for x2 in x['source']:
         pySource = pySource + x2
         if x2[-1] != '\n':
            pySource = pySource + '\n'

print(pySource)

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