[英]'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.