简体   繁体   English

将ipynb文件读入python

[英]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.目前,当单个文件的所有 id 都更新并且每个文件都是一个 ipynb 文件时,我的脚本会自动运行,我可以使用 %run 导入代码运行良好。

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.这里唯一的问题是我必须手动输入文件,并且如果我能够将文件读入我的 python 代码并使用正则表达式 function 或字符串操作我可以找到并获取密钥,那么我必须手动输入文件和 id 在文件中。

but i don't know how to read an ipynb file into my python code但我不知道如何将 ipynb 文件读入我的 python 代码

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.我找到了答案,我开始使用不同的打开方式我通过 ipynb 文件类型更多时间我了解它是一个 html 文件,以 json 格式编写,因此我们可以使用 ZFC35FDC70D5FC69D2639883A822C7A 方法读取它

 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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