How to convert .py to .ipynb in Ubuntu 22.04
- [jupyter nbconvert](#jupyter nbconvert)
- p2j
最近看到大家在用jupyter notebook,我也试了一下,感觉还不错,不过,也遇到了一些问题,比方说,我有堆的.py文件,如果要一个一个将其复制-粘贴到jupyter notebook中工作量将是非常大的,所以,我就在想有没一种方法可以让我能够实现.py文件到.ipynb文件的转换。
jupyter nbconvert
关于这个问题,我也试着问了一下ChatGPT
根据ChatGPT的回复,我尝试着用命令
bash
jupyter nbconvert --to notebook --execute utils.py --output utils.ipynb
转换,结果提示我的文件有问题,下面是我的原始文件内容:
bash
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$ cat utils.py
from pyalink.alink import *
import os
from datetime import datetime
from math import floor
ROOT_DIR = "/home/lwk/data/pyalink/"
def generateSchemaString(colNames, colTypes):
n = len(colNames)
str = ''
for i in range(n):
if i>0 :
str = str + ","
str = str + colNames[i] + " " + colTypes[i]
return str
def splitTrainTestIfNotExist(source, trainFilePath, testFilePath, ratio):
if not(os.path.exists(trainFilePath) or os.path.exists(testFilePath)):
spliter = SplitBatchOp().setFraction(ratio)
source.link(spliter)
spliter.link(
AkSinkBatchOp().setFilePath(trainFilePath)
)
spliter.getSideOutput(0)\
.link(
AkSinkBatchOp().setFilePath(testFilePath)
)
BatchOperator.execute()
class Stopwatch :
def __init__(self):
self.timer_start = datetime.now()
self.timer_end = datetime.now()
def start(self):
self.timer_start = datetime.now()
def stop(self):
self.timer_end = datetime.now()
def reset(self):
self.timer_start = datetime.now()
def getElapsedTimeSpan(self):
elapse = self.timer_end - self.timer_start
r = "";
if elapse.days > 0 :
r += str(elapse.days) + " days "
if elapse.seconds >= 3600 :
r += str(floor(elapse.seconds/3600)) + " hours "
if elapse.seconds >= 60 :
r += str(floor(elapse.seconds%3600/60)) + " minutes "
if elapse.seconds > 0 :
r += str(elapse.seconds%60) + " seconds "
if elapse.microseconds > 0 :
r += str(elapse.microseconds/1000) + " milliseconds "
return r
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$
这个问题暂时先不管了,我们用另外一种方式来转换。
p2j
这里,我们先来安装p2j包,即:
bash
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$ pip install p2j
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting p2j
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ee/5b/98fd10f7ba9fdfc99613c99040a87285dfc68392e5a059c22387ea814e39/p2j-1.3.2-py3-none-any.whl (19 kB)
Installing collected packages: p2j
Successfully installed p2j-1.3.2
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$
接下来,我们借助p2j来转换
bash
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$ p2j utils.py
Notebook utils.ipynb written.
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$
我们看到.pynb文件已经生成,接下来,我们在jupyter notebook中打开新生成的utils.ipynb文件,如下图所示。
至此,我们任务就算完成了。
参考文献