使用csv writer创建列的平均值

时间:2016-10-24 10:45:56

标签: python csv mean

我尝试生成使用csv writer保存的两列平均值。我有响应时间,答案是否正确。

代码:

stimTime = time.time()
if thisKey in ['a']:
    pmResponse = time.time()
    pmTime = pmResponse - stimTime
    print pmTime
    if sampled_word == sampled_word.upper():
        pmCue = "correct"
    else:
        print "incorrect"
    prospective_response = (pmTime, pmCue)
    with open (info['participant']+"pm", 'ab') as csvfile:
        wr = csv.writer(csvfile, delimiter=',')
        wr.writerow(prospective_response)

因此文本文件数据如下:

0.5339999198913574,correct
1.0839998722076416,correct
1.234234, incorrect

所以我试图弄清楚如何创建两列的平均值,并在csv文件的末尾写入。据我所知,我必须改为正确和不正确的数字形式。

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

为什么不添加此代码:

totalResponse += pmTime
rowCounter += 1
if sampled_word == sampled_word.upper():
    pmCue = "correct"
    correct += 1
else:
    print "incorrect"

并在一切结束时:

with open (info['participant']+"pm", 'ab') as csvfile:
    wr = csv.writer(csvfile, delimiter=',')
    wr.writerow(correct/float(rowCounter),totalResponse/rowCounter)