如何在jupyter中使用%% writefile -a命令在文件中添加换行符? [英] How to append a file with a newline using %%writefile -a command in jupyter?
问题描述
我使用Jupyter Notebook编写一个小程序.因此,我想将一些单元格写入.py文件.
I use a Jupyter Notebook to write a small program. Therefore, I want to write some cells into a .py file.
我使用魔法%%writefile -a myfile.py
.它可以工作,但是问题是内容没有新行添加.因此,我必须在运行单元格后手动向myfile.py
添加换行符.
I use this magic %%writefile -a myfile.py
. It works, but the problem is that the content is appended without a new line. So, I have to manually add a line break to myfile.py
after running the cells.
如何避免此问题?
推荐答案
只需在单元格中的%%writefile
之后放置一个空行.例如:
Just put an empty line after %%writefile
in the cell. For example:
In [1]:
%%writefile -a myfile.py
a = 1
In [2]:
%%writefile -a myfile.py
b = 2
现在:
In [2]:
%less myfile.py
a = 1
b = 2
在单元格的开头添加两个空行,以使之间的空行
myfile.py
文件中两个单元格的导出代码.
Add two empty lines to the beginning of a cell for an empty line between
the exported code of two cells in the file myfile.py
.
这篇关于如何在jupyter中使用%% writefile -a命令在文件中添加换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!