def convert_ipynb_to_py(ipynb_file, py_file): . f0 ` [( T7 A/ V& x9 d b4 p
with open(ipynb_file, 'r',encoding='utf-8') as f: - T3 n4 B- c\" |8 ^( z
notebook = json.load(f) 1 g& F2 |$ F( U8 `; K
* V) D' h% g) ?0 L7 a
with open(py_file, 'w',encoding='utf-8') as f:, ^7 t2 X( L9 P8 ^( _8 r2 w
for cell in notebook['cells']: ; s: C9 F/ p8 Q+ {
if cell['cell_type'] == 'code':0 x2 w: {) U0 H+ e3 Z2 b
f.write(''.join(cell['source']) + '\n\n'): E6 F+ J: E9 S; A
复制代码
以上代码是一个用于将.ipynb格式的Jupyter Notebook文件转换为.py格式的Python脚本的函数。该函数会读取指定的.ipynb文件,提取其中的代码单元格内容,并将其写入到指定的.py文件中。3 W$ s+ Z( M, ^% B
函数的参数解释如下:( D- T( G5 M7 s# n& ~% b
3 T" ]' d& o _
1.ipynb_file:输入的.ipynb文件路径。* {7 l" D- C' ^' T$ x) ~
2.py_file:输出的.py文件路径。6 b0 W+ ]5 o. u$ K' s