好,所以我有一个包含20个.csv文件的目录,如下所示:
我提供了一个zip文件夹,其中包含供您在此处使用的文件:
每个文件看起来像这样:
我需要做的是加载以.csv结尾的目录中的所有文件
然后,我需要打印一个简洁的表格,列出每个参与者反应时间的平均值和标准偏差(“ rt”是反应时间的列标题)。对于一致的结果和不一致的结果,应该分别进行此操作。我还需要打印每种条件(一致,不一致)的正确百分比,这可以使用正确列中的true和false值来计算。
输出应该看起来像这样:
仅作为旁注,我需要在不使用pandas模块的情况下进行此操作。
谢谢您的帮助。有任何问题请询问。
到目前为止,我所了解的并不多,但这是读取所有文件并将它们添加到files变量中:
from glob import glob
import os
from os.path import basename
import csv
import numpy as np
def main():
rts = {}
directory = 'C:/Users/oli.warriner/Desktop/data(2)/data/'
files = sorted(glob('C:/Users/oli.warriner/Desktop/data(2)/data/*.csv'))
sfiles = [basename(filepath) for filepath in files]
for f in sfiles:
path = os.path.join(directory, f)
singleFile = csv.DictReader(open(path, 'r'))
for line in singleFile:
if line['condition'] not in rts:
rts[ line['condition']] = []
rts[ line['condition']].append(float (line['rt']))
for condition in rts.keys():
data = np.array(rts[condition])
m = data.mean()
v = data.var()
if __name__ == "__main__":
main()
当前字典输出:
{'congruent': [0.647259, 0.720116, 0.562909, 0.538918, 0.633367, 0.668142, 1.820112, 0.798532, 0.470939, ...],
'incongruent': [0.767041, 0.990185, 0.693017, 0.679368, 0.951432, 1.289047, 0.647722, 0.858307, 1.118404, ...]}
所需的输出:
results = {'PO1':
{'Congruent':
{'rt': {0.4, 0.5, 0.8, 0.5, 0.6}},
{'correct': {TRUE, FALSE, TRUE, TRUE, FALSE}}
},
{'Incongruent':
{'rt': {0.4, 0.5, 0.8, 0.5, 0.6}},
{'correct': {TRUE, FALSE, TRUE, TRUE, FALSE}}
}
},
{'PO2':
{'Congruent':
{'rt': {0.4, 0.5, 0.8, 0.5, 0.6}},
{'correct': {TRUE, FALSE, TRUE, TRUE, FALSE}}
},
{'Incongruent':
{'rt': {0.4, 0.5, 0.8, 0.5, 0.6}},
{'correct': {TRUE, FALSE, TRUE, TRUE, FALSE}}
}
},
{'PO3':
{'Congruent':
{'rt': {0.4, 0.5, 0.8, 0.5, 0.6}},
{'correct': {TRUE, FALSE, TRUE, TRUE, FALSE}}
},
{'Incongruent':
{'rt': {0.4, 0.5, 0.8, 0.5, 0.6}},
{'correct': {TRUE, FALSE, TRUE, TRUE, FALSE}}
}
}
您的问题可以使用Python标准库解决。我建议您执行以下过程以及相关的对象和功能:
csv.writer
使用通配符迭代目录中的文件列表:csv.writer
使用CSV阅读器打开文件:pathlib.Path.glob
和pathlib.Path.glob
迭代CSV行以收集标记的数据:例如pathlib.Path.open
的pathlib.Path.open
对收集的数据进行基本统计:csv.reader
和csv.reader
dict
这应该完成工作。