繁体   English   中英

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

[英]'io.BufferedReader' object is not subscriptable' error

我正在为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

trials变量是一个文件对象(来自trials = open('cog2.csv', 'rb') ),并且您尝试使用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]))

暂无
暂无

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

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