如何将文本文件 (.py) 加载/编辑/运行/保存到 IPython 笔记本单元格中? [英] How to load/edit/run/save text files (.py) into an IPython notebook cell?

查看:31
本文介绍了如何将文本文件 (.py) 加载/编辑/运行/保存到 IPython 笔记本单元格中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 IPython 笔记本作为我工作流程的一部分.但是,我没有成功找到将 .py 文件导入到打开的 IPython 笔记本的各个单元格中的方法,以便它们可以进行编辑、运行和保存.可以这样做吗?

I've recently moved over to using IPython notebooks as part of my workflow. However, I've not been successful in finding a way to import .py files into the individual cells of an open IPython notebook so that they can edited, run and then saved. Can this be done?

我发现 这个文档告诉我如何将 .py 文件作为新笔记本导入,但这达不到我想要的效果.

I've found this in the documentation which tells me how to import .py files as new notebooks but this falls short of what I want to achieve.

如有任何建议,我们将不胜感激.

Any suggestions would be much appreciated.

推荐答案

EDIT:从 IPython 3(现在的 Jupyter 项目)开始,笔记本有一个文本编辑器,可以用作更方便的替代加载/编辑/保存文本文件.

EDIT: Starting from IPython 3 (now Jupyter project), the notebook has a text editor that can be used as a more convenient alternative to load/edit/save text files.

可以使用魔术命令 %load 在笔记本单元格中加载文本文件.

A text file can be loaded in a notebook cell with the magic command %load.

如果您执行包含以下内容的单元格:

If you execute a cell containing:

%load filename.py

filename.py 的内容将被加载到下一个单元格中.您可以照常编辑和执行它.

the content of filename.py will be loaded in the next cell. You can edit and execute it as usual.

要将单元格内容保存回文件,请在单元格开头添加 cell-magic %%writefile filename.py 并运行它.请注意,如果已存在同名文件它将被无声地覆盖.

To save the cell content back into a file add the cell-magic %%writefile filename.py at the beginning of the cell and run it. Beware that if a file with the same name already exists it will be silently overwritten.

要查看任何魔术命令的帮助,请添加 ?:例如 %load?%%writefile?.

To see the help for any magic command add a ?: like %load? or %%writefile?.

有关魔术函数的一般帮助,请键入%magic"有关可用魔法函数的列表,请使用 %lsmagic.对于描述其中任何一个,键入 %magic_name?,例如%cd?".

For general help on magic functions type "%magic" For a list of the available magic functions, use %lsmagic. For a description of any of them, type %magic_name?, e.g. '%cd?'.

另见:魔术函数来自官方 IPython 文档.

See also: Magic functions from the official IPython docs.

这篇关于如何将文本文件 (.py) 加载/编辑/运行/保存到 IPython 笔记本单元格中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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