'io.BufferedReader'对象不可下标'错误

时间:2019-03-22 21:21:01

标签: python psychopy

我正在为PsychoPy的实验编写Stroop任务。我正在尝试绘制图像和文本刺激,但是却收到错误消息(如下所示)。

我尝试查看google / stackoverflow页面,但不理解此错误消息(因此很难修复此代码)。

# ------Prepare to start Routine "instructions"-------
t = 0
instructionsClock.reset()  # clock
frameN = -1
continueRoutine = True
# update component parameters for each repeat
ready = event.BuilderKeyResponse()
# keep track of which components have finished
instructionsComponents = [instrText, ready]
for thisComponent in instructionsComponents:
    if hasattr(thisComponent, 'status'):
        thisComponent.status = NOT_STARTED

#read stimuli file
trials = open('cog2.csv', 'rb')
imageFile = 0     #imageFile = trials[trialNumber][Column]
corrAns = 1       #corrAns = trials[trialNumber][Column]
Congruent = 2     #Congruent = trials[trialNumber][Column]
stimCat = 3       #stimCat = trials[trialNumber][Column]
Superimposed = 4  #Superimposed = trials[trialNumber][Column]
Word = 5          #word = trials[trialNumber][Column]

#turn the text string into stimuli
textStimuli = []
imageStimuli = []
for trial in trials:
    textStimuli.append(visual.TextStim(win, text=trials[Word]))  <---- ERROR
    imageStimuli.append(visual.ImageStim(win, size=[0.5, 0.5], image=trials[imageFile]))

我正在尝试从我上传的Excel文档中写出绘画刺激(包含jpg图像的路径以及要叠加在图像上的单词)。

不过,目前,我收到错误消息:

#### Running: C:\Users\Sophie\OneDrive\Spring '19\Research\PsychoPy\Bejj\Test_3_22_19.py #####
Traceback (most recent call last):
  File "C:\Users\Sophie\OneDrive\Spring '19\Research\PsychoPy\Bejj\Test_3_22_19.py", line 203, in <module>
    textStimuli.append(visual.TextStim(win, text=trials[Word]))
TypeError: '_io.BufferedReader' object is not subscriptable

1 个答案:

答案 0 :(得分:1)

trials变量是一个文件对象(来自trials = open('cog2.csv', 'rb')),您正尝试使用trials[Word]作为列表访问它,因此会出错。

您应该使用csv.reader方法代替以CSV格式读取文件,以便将trial分配给每一行作为列表,并且您可以在访问索引的同时访问每一列预期的:

import csv
for trial in csv.reader(trials):
    textStimuli.append(visual.TextStim(win, text=trial[Word]))
    imageStimuli.append(visual.ImageStim(win, size=[0.5, 0.5], image=trial[imageFile]))