'io.BufferedReader'对象不可订阅'错误

问题描述 投票:0回答:1

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

我曾尝试查看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
python psychopy
1个回答
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]))
© www.soinside.com 2019 - 2024. All rights reserved.