首页 > 解决方案 > Python:计算值并发送电子邮件

问题描述

更新:我已经更正了我的代码,下面的工作正常

基本上我需要像下面这样的邮件输出。我做到了这一点。但需要知道是否有任何有效的代码然后低于一个。

名称 5001 5010 9000 4 %

名称 5002 5010 9000 4 %

名称 5003 5010 9000 4 %

名称 5004 5010 9000 4 %

  1. 将值存储在列表中。

以下是虚拟值

container = []
for server in range(1,5):
    container.append('name')
    container.append(server + 5000)
    container.append(5000+10)
    container.append(4000+5000)
    container.append(2500 % 12)

    print('\n' + str(container))
  1. 将值列表分配给 msgBody 以便通过电子邮件发送

我只是在这里放一段代码。下面也工作正常

msgBody1 = ''

对于范围内的计数(4):

if count == 0:
    tempValue = '\n' + '\n' + str(container[count]) + '\t' + str(container[count+1]) + '\t' + str(container[count+2]) + '\t' 
    + str(container[count+3]) + '\t' + str(container[count+4])
    msgBody1 = msgBody1 + str(tempValue) + ' %'


elif count == 1:
    tempValue = '\n' + '\n' + str(container[count+4]) + '\t' + str(container[count+5]) + '\t' + str(container[count+6]) + '\t' 
    + str(container[count+7]) + '\t' + str(container[count+8])
    msgBody1 = msgBody1 + str(tempValue) + ' %'


elif count == 2:
    tempValue = '\n' + '\n' + str(container[count+8]) + '\t' + str(container[count+9]) + '\t' + str(container[count+10]) + '\t' 
    + str(container[count+11]) + '\t' + str(container[count+12])
    msgBody1 = msgBody1 + str(tempValue) + ' %'


elif count == 3:
    tempValue = '\n' + '\n' + str(container[count+12]) + '\t' + str(container[count+13]) + '\t' + str(container[count+14]) + '\t' 
    + str(container[count+15]) + '\t' + str(container[count+16])
    msgBody1 = msgBody1 + str(tempValue) + ' %'

任何其他更好和更短的代码来替换 msgBody1

提前致谢

标签: pythonpython-3.xpython-2.7python-requests

解决方案


你的问题不清楚;代码示例没有任何意义。但是从它的结构来看,您似乎正在尝试使用 dict,但您正在定义或采购列表。

不知道为什么for server in servers,我希望你的服务器列表是数值的集合,这没有任何意义。

请仔细阅读 list Vs dict 和 list.append() 以及如何将新的键、值对添加到字典中。


推荐阅读