我正在开发一个scraper,它可以检查文件传输的进度,它可以根据IP地址的pickle列表进行操作。当一个文件完成时,我希望IP地址从IP的pickle列表中删除,并移动到一个单独的pickle列表中,“complete”。在
开始时间:
servers = {"10.10.10.1": "", "10.10.10.2": "", "10.10.10.3": ""}
skeys = servers.keys()
complete = []
def donewith(server):
if server in skeys:
complete.append("{0}".format(server))
servers.pop("{0}".format(server))
logging.info('Moved {0} to Complete list.'.format(server))
else:
logging.error('Unable to move \'{0}\' to Complete list.'.format(server))
期望结果:
^{pr2}$这就是我真正得到的。在
donewith(10.10.10.1)
File "<stdin>", line 1
donewith(10.10.10.1)
^
SyntaxError: invalid syntax
或者用引号调用函数会产生一个TypeError requiring an integer.
不太确定如何解决这个问题,因为它看起来是一个如此简单的问题
报价方案阐述:
def check(server):
#print "Checking {0}".format(server)
logging.info('Fetching {0}.'.format(server))
response = urllib2.urlopen("http://"+server+"/avicapture.html")
tall = response.read() # puts the data into a string
html = tall.rstrip()
match = re.search('.*In Progress \((.*)%\).*', html)
if match:
temp = match.group(1)
results = temp
servers[server] = temp
if int(temp) >= 98 and int(temp) <= 99:
abort(server)
alertmail(temp, server)
donewith(server)
logging.info('{0} completed.'.format(server))
return str(temp)
else:
logging.error('Unable to find progress for file on {0}.'.format(server))
return None
此函数调用donewith()
函数,如果该函数有引号,例如:donewith("server")
,则该函数不起作用。在
示例:
def check(server):
donewith("server")
def donewith(server)
do_things.
check(server)
导致。。在
check(10.10.10.3)
^
SyntaxError: invalid syntax
总是在第三组数字中有零。。。在
键是一个字符串。你应该这样做:
您想要的行为是不可能的,因为
10.10.10.1
不是一个字符串,而且python看到点.
,它试图将其解析为float,但是float只能有一个小数部分,这就是为什么异常指向第二个点之后的数字的原因。在要使其工作,您需要将参数作为字符串传递:
不仅在}中:
^{pr2}$donewith
中,而且在{因为python就是这样工作的,所以需要用引号声明字符串,否则它将被视为数字(第一个字符是数字)或变量名。在
试试这个
相关问题 更多 >
编程相关推荐