【发布时间】:2021-08-12 18:47:46
【问题描述】:
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(**)>
但是,我收到以下错误:
“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 文件。
-
如果 cvs 文件中的第一行是标题,您可以跳过它并从第二行开始循环
for row in reader[1:]: -
列应用
-
嗨 Yuri,我已经编辑了帖子,请使用上面的链接访问 csv 文件的图像,链接是:我还有一个 csv 格式的 excel 文件,其中包含尺寸(在第一列) 和矩形的颜色(第二种颜色)
标签: python loops for-loop rectangles rect