如何在jupyter笔记本上将python脚本另存为.py文件 [英] How to save python script as .py file on jupyter notebook

查看:645
本文介绍了如何在jupyter笔记本上将python脚本另存为.py文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Jupyter笔记本上编写了一些函数,并希望将脚本保存为.py格式,但是anaconda jupyter笔记本会自动将脚本另存为.ipynb,如何针对此特定脚本将其更改为.py?有任何命令吗?谢谢

I wrote some functions on Jupyter notebook and want to save the script in form of .py, but anaconda jupyter notebook automatically saves the script as .ipynb, How can I change it to .py for this specific script? Is there any command for it? Thanks

推荐答案

Jupyter中的内置Magic命令:

IPython具有一组预定义的魔术函数",您可以调用这些函数 使用命令行样式的语法.有两种魔术, 面向行和面向单元.线魔术以%为前缀 角色和工作方式非常类似于OS命令行调用:它们作为 其余的参数,在不传递参数的情况下 括号或引号.线魔术可以返回结果并可以使用 在作业的右侧.细胞魔法以 两倍的%%,而且它们是不仅作为参数而获得的函数 该行的其余部分,以及下面的单独行 论点.

IPython has a set of predefined ‘magic functions’ that you can call with a command line style syntax. There are two kinds of magics, line-oriented and cell-oriented. Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes. Lines magics can return results and can be used in the right hand side of an assignment. Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.

魔术作为有用的函数非常有用,而Python语法却没有 最自然的一种,或者当一种想要嵌入无效的python语法时 在他们的工作流程中.

Magics are useful as convenient functions where Python syntax is not the most natural one, or when one want to embed invalid python syntax in their work flow.

%% writefile (写出单元格保存到文件)

%%writefile (Write the contents of the cell to a file)

%save (保存一组行或给定文件名的宏)

%save (Save a set of lines or a macro to a given filename)

这篇关于如何在jupyter笔记本上将python脚本另存为.py文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆