是否可以在IPython Notebook中的%% writefile magic命令中写入变量的值?

bur*_*res 7 python ipython-notebook

我希望能够使用变量在IPython Notebook中输出一些文本。我可以使用%% writefile命令从IPython Notebook中写入文件。我还可以使用变量使用$ varaible_name命名文件。

file_name = "file.out"
some_variable = "text"
Run Code Online (Sandbox Code Playgroud)

新细胞

%%writefile $file_name
????some_varaible  #<-what command goes here
Run Code Online (Sandbox Code Playgroud)

这样输出文件(名为“ file.out”)看起来像这样:

文本

And*_*eyT 5

现在可以使用以下命令来实现:

%store varname >filename.txt
Run Code Online (Sandbox Code Playgroud)

有关 %store 的更多信息