私はCSVファイルを解析するはずのPythonコードをいくつか書いています。特定の行の平均値を計算し、CSVファイルの1行の値に基づいて受信者に電子メールを送信します。次のように私のコードは、私は今のコードを送信した電子メールを削除し、print文に置き換えました:リストの補完を実行した後でループがdictで機能しない
def main():
#smtp_instance = smtplib.SMTP('localhost')
ldap_file = open('ldaps.csv','rU')
ldap_data=csv.DictReader(ldap_file)
scores = list(int(d['score']) for d in ldap_data)
average_score = sum(scores)/len(scores)
print average_score
for rows in ldap_data:
ldap = rows['ldap']
fullname = rows['fullname']
firstname = fullname.split(' ')[0]
location = rows['location']
score = rows['score']
if int(score) < average_score:
score_msg = 'below'
else:
score_msg = 'above'
print 'Hi ' + firstname + '\n'\
'You got a ' + score + '% on your Final Exam.'\
'The average score was ' + average_score + '.'\
'This means that you scored ' + score_msg + ' average.'
if __name__ == '__main__':
main()
私はこれを実行すると、それはaverage_score値を出力します。しかしコードはfor..loopに決して行きません。私はディクテーションでリスト内包を呼び出すことはできないと思われ、同じディクテーションで繰り返します。どのようなアイデアを私は間違っていると私はそれを修正することができますか?
後のリストにそれをコピーしてください。あるいは、 'pdb'を使ってステップを進め、同じことをチェックしてください。 – Useless