如何解决精神病学中的简单矩形实验
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 文件中。我认为它是用带有 BOM 签名的 UTF-8 编码保存的。尝试更改代码中的一行:
datafile = open("stimuli.csv","rt")
到
datafile = open("stimuli.csv","r",encoding="utf-8-sig")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。