psychopy.org | Reference | Downloads | Github

Calculating means and standard deviations from multiple .csv data files

#1

I am trying to collect means and standard deviations of reaction times from my data files, created using PsychoPy. However, when I first try to split a line from the file with line.split(’,’), I get the error saying there are too many values to unpack. If I then implement some of the solutions I have seen online e.g. putting the values into a for loop -> for (trialnum, rt) in line.split(’,’): <- I get another error that says ‘need more than 1 value to unpack’. Please could someone help me fix this! :slight_smile:

my code:

import glob, os 

#prints file name in Data folder in order (ppt1-ppt20)
os.chdir("/Users/ameliashelton/Documents/Year 3 /Programming in Neuroimaging/Assessment 2/data")
rts = []

for file in sorted(glob.glob("*.csv")):
    f = open (file, 'r')
    f.readline()
    for line in f.readlines():
        trialnum, rt =  line.split(',')
        rt = float(rt)
        rts.append(rt)
        
        
rts = np.array(rts)

rt_mean = rts.mean()
rt_std = rts.std()
    
rt_ntrials = len(rts)

#print in correct format 
print("RT Mean : {:.2f}seconds".format(rt_mean))
print("RT Std : {:.2f}seconds".format(rt_std))
print("Num RTs : {}".format(rt_ntrials))  ```
0 Likes

#2

Hello Amelia,

Please provide a sample of the CSV file (the first few lines).

0 Likes

#3

57

0 Likes

#4

When Python splits the line, 7 items are being returned, but you are trying to pack them into two. You need to discard the values you are not using like this:

for file in sorted(glob.glob("*.csv")):
    f = open (file, 'r')
    f.readline()
    for line in f.readlines():
        trialnum, _, _, _, _, rt, _ = [i.strip() for i in line.split(',')]  # strip away any spaces or special characters
        rt = float(rt)
        rts.append(rt)
0 Likes

#5

thank you very much. can’t believe I was being that stupid!

0 Likes