Python 参数不带 $ 符号,如何实现?
Python Arguments not taking $ symbol, how to acheive this?
Python代码test.py
:
import sys
username = str(sys.argv[1])
password = str(sys.argv[2])
print username, password
执行#1:python test.py usr@gmail.com pa$$word
结果 #1:usr@gmail.com, pa24481word
预期#1:usr@gmail.com, pa$$word
执行#2:python test.py usr@gmail.com $word@pass
结果 #2:usr@gmail.com, @pass
预期#2:usr@gmail.com, $word@pass
我只想在我的代码中将我传递的内容作为字符串提供。
机器详细信息:Python 2.6 on OEL 6.6
如果有人可以提供通用的解决方案 OS - 那就太好了!
这与Python无关。您的 shell 在将值传递给脚本之前正在对其进行解释; $
in bash 引入环境变量。
您必须用单引号将参数括起来以防止出现这种情况:
python test.py usr@gmail.com 'pa$$word'
Python代码test.py
:
import sys
username = str(sys.argv[1])
password = str(sys.argv[2])
print username, password
执行#1:python test.py usr@gmail.com pa$$word
结果 #1:usr@gmail.com, pa24481word
预期#1:usr@gmail.com, pa$$word
执行#2:python test.py usr@gmail.com $word@pass
结果 #2:usr@gmail.com, @pass
预期#2:usr@gmail.com, $word@pass
我只想在我的代码中将我传递的内容作为字符串提供。
机器详细信息:Python 2.6 on OEL 6.6
如果有人可以提供通用的解决方案 OS - 那就太好了!
这与Python无关。您的 shell 在将值传递给脚本之前正在对其进行解释; $
in bash 引入环境变量。
您必须用单引号将参数括起来以防止出现这种情况:
python test.py usr@gmail.com 'pa$$word'