问题描述
在笔记本计算机的命令行中使用变量时,我看到我们可以在变量前使用$,或使用{}包围变量,例如
In using variables in the command line from a notebook cell, I saw that we can use put a $ in front of the variable, or surround the variable using {} , for example
!command {variable}
或
!command $variable
但是,当我在笔记本计算机中使用命令行运行python脚本时,会出现错误
But when I was running a python script using the command line from a notebook cell, I would get errors
variable1 = '/path/to/directory'
variable2 = 7
!Script.py -arg1 $variable1 -arg2 $variable2
和
!Script.py -arg1 {variable1} -arg2 {variable2}
不起作用.
稍作试验后,我发现如果变量是字符串,则用引号将整个arg括起来即可.
After experimenting a little bit, I found that if a variable is a string, surrounding the whole arg with quotes got it to work.
variable1 = '/path/to/directory'
variable2 = 7
!Script.py -arg1 '$variable1' -arg2 $variable2
这是怎么回事?我试图查找这种现象,但找不到任何东西.
What is going on? I tried to look up this phenomena but I couldn't find anything.
如果有所作为,我正在使用google colab colaboratory
If it makes a difference, I am using google colab colaboratory
推荐答案
您尝试过吗?
!Script.py -arg1 $variable1\ -arg2 $variable2\
这篇关于笔记本:使用引号和$将字符串变量作为参数通过命令行(!)传递给python脚本,例如-arg1'$ varible1'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!