繁体   English   中英

从 csv 文件中提取的值创建复杂的嵌套字典。 - Python

[英]Creating complex nested dictionary from values pulled from csv file. - Python

我有以下文件:

文件

我需要能够构建一个字典并用来自多个 csv 文件的数据填充它

每个文件内部显示如下:

在此处输入图片说明

所以我的问题是,我需要逐行收集目录中的所有文件,并将相关数据分离到字典中,如图所示。

这是我到目前为止所拥有的 - 我正在设法遍历目录并获取每个单独的文件名,但是我不确定如何在进入字典时逐行读取解析所需的数据。

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 f not in rts:
                rts[f] = []
                if line['condition'] not in rts[f]:
                    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}}           
        }
       }

我需要能够在每个条件中嵌套 rt 值以及每个条件的正确值。 这也需要由文件分隔并存储在字典中。

我将使用字典计算每个条件的均值和标准偏差反应时间。 任何帮助是极大的赞赏。 如果我在任何地方过于含糊,请不要犹豫,提出问题

下面的代码将使用您期望的值填充rts字典。 不过,我特意将numpy部分留在了外面。 我想从那里接你不会有问题。

由于pathlib它需要 Python pathlib

import csv
from pathlib import Path


def main():
    rts = {}

    data = Path('C:/Users/oli.warriner/Desktop/data(2)/data')

    for csvfile in data.glob('*.csv'):
        key = csvfile.stem

        with csvfile.open() as f:
            csv_reader = csv.reader(f)

            # Skip the header
            _ = next(csv_reader)

            rts[key] = {
                'congruent': {
                    'rt': [],
                    'correct': []
                },
                'incongruent': {
                    'rt': [],
                    'correct': []
                },
            }

            for tn, ctext, cname, condition, response, rt, correct in csv_reader:
                rts[key][condition]['rt'].append(float(rt))
                rts[key][condition]['correct'].append(correct)

我希望它作为一个起点有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM