【问题标题】:Python : Calculate values and send in emailPython:计算值并发送电子邮件
【发布时间】:2019-02-28 15:40:31
【问题描述】:

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

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

名称 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

提前致谢

【问题讨论】:

  • 看起来您将整数索引与列表(必须是数字)和字典混合在一起。 server 是什么?一个号码?细绳?请提供示例输入。但基本上这是失败的,因为最初程序正在尝试访问 a[0](因为 count==0),而 a 是一个空列表。

标签: python python-3.x python-2.7 python-requests


【解决方案1】:

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

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

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

【讨论】:

  • 我已经更新了我的问题。你能建议任何可行的代码吗?
猜你喜欢
  • 2020-07-25
  • 2022-10-13
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多