字符串格式函数不解析我的斜杠字符?
String format function does not parse my slashes character?
我正在做一个小的 python 脚本来执行 wget 调用,但是当我替换包含 url/ip 地址的字符串时遇到问题,它将被提供给我的 "wget" 字符串
import os
import sys
usr = sys.argv[1]
pswd = sys.argv[2]
ipAddr = sys.argv[3]
wget = "wget http://{IPaddress}"
wget.format(IPaddress=ipAddr)
print "The command wget is %s" %wget
os.system(wget)
如果我 运行 该脚本我得到下面的代码片段,我知道 wget 失败了,因为变量 ipAddr 没有替换 IPaddress 模式,所以我猜这个问题与中的斜线有关url。我的问题是为什么那个模式没有被替换?
python test.py 1 2 www.website.org The command wget is wget http://{IPaddress}
--2015-12-03 20:26:11-- http://%7Bipaddress%7D/
Resolving {ipaddress} ({ipaddress})... failed: Name or service not known.
您没有将 format
调用的结果分配给任何东西 - 您只是将其丢弃。试试这个:
wget = "wget http://{IPaddress}"
wget = wget.format(IPaddress=ipAddr)
print "The command wget is %s" %wget
os.system(wget)
或者,这看起来更干净一些:
wget = "wget http://{IPaddress}".format(IPaddress=ipAddr)
print "The command wget is %s" %wget
os.system(wget)
您实际上需要像这样格式化您的 wget
字符串。
import os
import sys
usr = sys.argv[1]
pswd = sys.argv[2]
ipAddr = sys.argv[3]
wget = "wget http://{IPaddress}".format(IPaddress=ipAddr)
print "The command wget is %s" % wget
os.system(wget)
.format()
不会就地修改字符串,它 returns 是修改后字符串的副本,因此在您的原始脚本中, wget.format(IPaddress=ipAddr)
的值实际上从未分配给任何变量和wget
变量的内容保持不变
我正在做一个小的 python 脚本来执行 wget 调用,但是当我替换包含 url/ip 地址的字符串时遇到问题,它将被提供给我的 "wget" 字符串
import os
import sys
usr = sys.argv[1]
pswd = sys.argv[2]
ipAddr = sys.argv[3]
wget = "wget http://{IPaddress}"
wget.format(IPaddress=ipAddr)
print "The command wget is %s" %wget
os.system(wget)
如果我 运行 该脚本我得到下面的代码片段,我知道 wget 失败了,因为变量 ipAddr 没有替换 IPaddress 模式,所以我猜这个问题与中的斜线有关url。我的问题是为什么那个模式没有被替换?
python test.py 1 2 www.website.org The command wget is wget http://{IPaddress}
--2015-12-03 20:26:11-- http://%7Bipaddress%7D/
Resolving {ipaddress} ({ipaddress})... failed: Name or service not known.
您没有将 format
调用的结果分配给任何东西 - 您只是将其丢弃。试试这个:
wget = "wget http://{IPaddress}"
wget = wget.format(IPaddress=ipAddr)
print "The command wget is %s" %wget
os.system(wget)
或者,这看起来更干净一些:
wget = "wget http://{IPaddress}".format(IPaddress=ipAddr)
print "The command wget is %s" %wget
os.system(wget)
您实际上需要像这样格式化您的 wget
字符串。
import os
import sys
usr = sys.argv[1]
pswd = sys.argv[2]
ipAddr = sys.argv[3]
wget = "wget http://{IPaddress}".format(IPaddress=ipAddr)
print "The command wget is %s" % wget
os.system(wget)
.format()
不会就地修改字符串,它 returns 是修改后字符串的副本,因此在您的原始脚本中, wget.format(IPaddress=ipAddr)
的值实际上从未分配给任何变量和wget
变量的内容保持不变