가볍게 배우고 깊게 즐기고 오래 남기기

[Pyton || Tip] nbformat 라이브러리를 활용한 노트북파일(ipynb) py변환 본문

Programming & Tip/Python

[Pyton || Tip] nbformat 라이브러리를 활용한 노트북파일(ipynb) py변환

Awesomist 2024. 5. 20. 14:03
728x90
import nbformat


notebook_path = 'name.ipynb'
with open(notebook_path, 'r', encoding='utf-8') as f:
    notebook = nbformat.read(f, as_version=4)


code_cells = [cell for cell in notebook.cells if cell.cell_type == 'code']


script_content = "\n\n".join(cell.source for cell in code_cells)

# Save the script to a .py file
script_path = 'name.py'
with open(script_path, 'w', encoding='utf-8') as f:
    f.write(script_content)

script_path

https://github.com/jupyter/nbformat

 

GitHub - jupyter/nbformat: Reference implementation of the Jupyter Notebook format

Reference implementation of the Jupyter Notebook format - jupyter/nbformat

github.com

 

반응형
Comments