心理学中的简单矩形实验

Simple rectangle experiment in psychopy

enter image description here我正在尝试改编现有的脚本来创建一个非常简单的心理实验,目的是让一个具有不同颜色和尺寸的矩形在屏幕上循环。

<**from psychopy import core, visual, event
import csv
  
## Setup section, read experiment variables from file
win = visual.Window([400,300], monitor="testMonitor", units="cm", fullscr=False)
stimuli = []
datafile = open("stimuli.csv", "rt")
reader = csv.reader(datafile, delimiter=";")
for row in reader:
    if len(row)==2:         # ignore empty and incomplete lines
        size = float(row[0])  # the first element in the row converted to a floating point number
        color = row[1]        # the second element in the row
        stimulus = visual.Rect(win, width=size, height=size)
        stimulus.fillColor = color
        stimuli.append(stimulus)
datafile.close()
  
## Experiment Section, use experiment variables here
for stimulus in stimuli:
    stimulus.draw()
    win.flip()
    core.wait(1.000)
 
## Closing Section
win.close()
core.quit(**)>

I also have an excel file in csv formt which contains the dimensions (in first column) and color (second color) of the rectangel

但是,我收到以下错误:

“File “C:\Users\USER\OneDrive\Área de Trabalho\psychopy\new_coding\untitled.py”, line 11, in

size = float(row[0]) # the first element in the row converted to a floating point number

ValueError: could not convert string to float: ‘3’

Experiment ended.

我附上脚本和 excel 文件(其中包含矩形尺寸和颜色)。我尝试了几种选择,包括更改 excel 文件中不带小数的尺寸,但错误消息保留。

我看你的代码没有问题。

问题很可能出在您的 CSV 文件中。我认为它是使用带有 BOM 签名的编码 UTF-8 保存的。尝试更改代码中的一行:

datafile = open("stimuli.csv", "rt")

datafile = open("stimuli.csv", "r", encoding="utf-8-sig")