我正在编写代码来起草一封电子邮件给 1 到 4 个收件人之间的任何地方。收件人是从 Excel 工作簿中提取的。因此,如果只有 3 个收件人,Person4 的单元格将为空白。问题是,每次有不同数量的收件人时,为一封单独的电子邮件编写代码似乎不是很 Pythonic。我想为一封电子邮件编写一个代码块,如果它是 None 则忽略一个被调用的变量
# collect recipent names from workbook
Person1 = sheet['BG1'].value
Person2 = sheet['BH1'].value
Person3 = sheet['BI1'].value
Person4 = sheet['BJ1'].value
# draft email
if Person4 != None:
print('Dear ' + Person1 + ', ' + Person2 + ', ' + Person3 + ', and ' + Person4 + ',\n' +
'The faculty members of ... *continued body of email*')
elif Person3 != None:
print('Dear ' + Person1 + ', ' + Person2 + ', and ' + Person3 + ',\n' +
'The faculty members of ... *continued body of email*')
elif Person2 != None:
print('Dear ' + Person1 + ', and ' + Person2 + ',\n' +
'The faculty members of ... *continued body of email*')
else:
print('Dear ' + Person1 + ',\n' +
'The faculty members of ... *continued body of email*')
有没有更聪明的方法来写这个?任意数量的收件人只有一个代码块?
30秒到达战场
相关分类