我正在从web服务中切割一些数据,其中一个参数是last_ping_time,它可以在某一点上没有值。我将当前的数据与last_ping_time进行比较,以检查与机器的通信。所以我得到了
ValueError:时间数据“无”不匹配格式'%Y-%m-%dT%H:%M:%SZ‘
如果没有,我试着设置虚拟值。
if ping=="None":
ping="2013-01-01T00:00:00尝试了,也打破或继续螺母,他们似乎不工作,这个错误持续存在。我需要循环继续到下一个值,如果为null或放置一些虚拟值,并继续。我没有主意了,请帮帮忙。
with open(filename, 'wb') as csvfile: #Creating report file
spamwriter = csv.writer(csvfile, delimiter=' ')
for computer in computers:
ping=computer["last_ping_time"]
ping=str(ping)
ping.split('T')
if ping=="None":
#break
ping="2013-01-01T00:00:00"
else
ping=datetime.datetime.strptime(ping, '%Y-%m-%dT%H:%M:%SZ') # Convert string to data
if ping<Current_Date:
#toremove=count+1
os.system('zenity --info --text="Computers not contacting more than 30 days:%s "' % (computer["hostname"]))
#print "Needs to be deleted" #Control Variable
spamwriter.writerow((computer["id"], computer["hostname"], computer["title"], computer["last_ping_time"], "30 Days from last contact machine will be be removed"))
else:
network=str(computer) #list to string
ip=network.split("u'ip_address': u'")[1] #1 shows what is after parameters
ip=ip.split("'")[0] #0 shows what is before parameters
mac=network.split("u'mac_address': u'")[1]
mac=mac.split("'")[0]
print mac
print ip
spamwriter.writerow((computer["id"], computer["hostname"], computer["title"], computer["last_ping_time"], mac, ip))
os.system('zenity --info --text="Report is created with name:%s "' % (filename))发布于 2013-02-19 16:25:47
删除ping = str(ping)行并使用:
if ping is None:
# no last ping timehttps://stackoverflow.com/questions/14961941
复制相似问题