【问题标题】:Simple rectangle experiment in psychopy心理学中的简单矩形实验
【发布时间】: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(**)>

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 文件。
  • 如果 cvs 文件中的第一行是标题,您可以跳过它并从第二行开始循环 for row in reader[1:]:
  • 列应用
  • 嗨 Yuri,我已经编辑了帖子,请使用上面的链接访问 csv 文件的图像,链接是:我还有一个 csv 格式的 excel 文件,其中包含尺寸(在第一列) 和矩形的颜色(第二种颜色)

标签: python loops for-loop rectangles rect


【解决方案1】:

我认为您的代码没有问题。

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

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
相关资源
最近更新 更多