Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以在IPython笔记本中的%%writefile magic命令中写入变量的值?_Python_Ipython Notebook - Fatal编程技术网

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

是否可以在IPython笔记本中的%%writefile magic命令中写入变量的值?,python,ipython-notebook,Python,Ipython Notebook,我希望能够使用变量在IPython笔记本中输出一些文本。我可以使用%%writefile命令从IPython笔记本中写入文件。我还可以使用一个变量,使用$varaible\u name来命名文件 file_name = "file.out" some_variable = "text" 新细胞 %%writefile $file_name ????some_varaible #<-what command goes here %%writefile$文件名 有些变量现在可以使用以下命

我希望能够使用变量在IPython笔记本中输出一些文本。我可以使用%%writefile命令从IPython笔记本中写入文件。我还可以使用一个变量,使用$varaible\u name来命名文件

file_name = "file.out"
some_variable = "text"
新细胞

%%writefile $file_name
????some_varaible  #<-what command goes here
%%writefile$文件名

有些变量现在可以使用以下命令:

%store varname > filename.txt

根据github线程,这是可能的,但使用自定义魔术命令:

from IPython.core.magic import register_line_cell_magic

@register_line_cell_magic
def writetemplate(line, cell):
    with open(line, 'w') as f:
        f.write(cell.format(**globals()))
用法:

some_variable = "text"
%%writetemplate filename.txt
test: {some_variable}

同时在github上提问被视为坏习惯,请至少等待几个小时,否则人们会在看到您的消息两次时感到恼火。例如,如果这是一个xml配置文件呢!