将jupyter转换为python文件

一直在思考是否可以将jupyter转化为python,一直都未尝试,但是最近在网上找了一些资料,尝试了一下。相应的代码如下:

# 简单的将jupyter转换为python文件
import argparse
import os
import subprocess
def convert(input_path, output_path):
    subprocess.call(['jupyter', 'nbconvert', '--to', 'script', input_path, '--output', output_path])
def cleanup(path):
    skip_lines_startwith = ('Image(filename=', '# In[', '# <hr>', 'from IPython.display import Image', 'get_ipython()', '# <br>')
    clean_content = []
    imports = []
    existing_imports = set()
    with open(path, 'r', encoding="utf8") as f:
        next(f)
        next(f)
        for line in f:
            line = line.rstrip(' ')
            if line.startswith(skip_lines_startwith):
                continue
            if line.startswith('import ') or (
                    'from ' in line and 'import ' in line):
                if 'from __future__ import print_function' in line:
                    if line != imports[0]:
                        imports.insert(0, line)
                else:
                    if line.strip() not in existing_imports:
                        imports.append(line)
                        existing_imports.add(line.strip())
            else:
                clean_content.append(line)
    clean_content = ['# coding: utf-8\n\n\n'] + imports + clean_content
    with open(path, 'w', encoding="utf8") as f:
        for line in clean_content:
            f.write(line)
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Convert Jupyter notebook to Python script.', formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('-i', '--input', required=True, help='Path to the Jupyter Notebook file')
    parser.add_argument('-o', '--output', required=True, help='Path to the Python script file')
    parser.add_argument('-v', '--version', action='version', version='v. 0.1')
    args = parser.parse_args()
    convert(input_path=args.input,output_path=os.path.splitext(args.output)[0])
    cleanup(args.output)

后续如果需要将python转换为jupyter的文件,在更新相应的代码

相关推荐

  1. jupyter转换python文件

    2024-01-02 11:48:02       35 阅读
  2. pythonvisio转换 PDF 文件

    2024-01-02 11:48:02       11 阅读
  3. 如何在 Python语音转换文本

    2024-01-02 11:48:02       27 阅读
  4. python 普通文件转换ts文件,用udp-ts 发送

    2024-01-02 11:48:02       31 阅读
  5. 字符串转换Python数据类型

    2024-01-02 11:48:02       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-02 11:48:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-02 11:48:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-02 11:48:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-02 11:48:02       20 阅读

热门阅读

  1. LeetCode 20. 有效的括号

    2024-01-02 11:48:02       37 阅读
  2. Android 10.0 截屏流程

    2024-01-02 11:48:02       32 阅读
  3. Atlas Hook 导入 Hive 元数据

    2024-01-02 11:48:02       33 阅读
  4. KVM虚拟机部署K8S重启后/etc/hosts内容丢失

    2024-01-02 11:48:02       39 阅读
  5. 深入理解@Resource与@Autowired:用法与区别解析

    2024-01-02 11:48:02       37 阅读
  6. JDK下载地址

    2024-01-02 11:48:02       48 阅读
  7. GRU算法

    GRU算法

    2024-01-02 11:48:02      34 阅读