0

我正在使用使用 Python 代码的 PsychoPy 创建一个神经心理学任务。我正在创建一个 GoNoGo 任务并有一个刺激列表(正面、负面和中性词)。我有一定数量的块(18),单词的目标和干扰价在每个块内都是固定的(即,块 1 = 目标:积极,干扰:中性等)。在每个块内,有 8 个目标词和 8 个干扰词以随机顺序出现(无需替换)。

所以,我想让psychopy从每个块的目标(8个词)和干扰项(8个词)的相关价中随机选择16个词。

我对代码没有经验,但想知道是否有人可以帮助我处理这个特定的代码。

为了让psychopy随机选择每个块的单词,我想要一个代码,要求psychopy从特定位置读取excel文件,随机打乱该文件其中一列中的单词,将该列中的所有单词拆分为8 个单词的块,然后重复 18 次,最后将这些新的 8 个单词块写入一个新文件。

我想要一个要求程序从特定位置读取 excel 文件的代码,随机打乱该文件中某一列中的单词,将该列中的所有单词拆分为 8 个单词的块,然后重复此操作18 次,最后将这些 8 个单词的新块写入一个新文件。

到目前为止,这是我的代码:

file.read(pathnameOfExcelFile.xlsx)
List=[excel column name]
Random.shuffle(list)
NewList=(List) #8words to be randomly chosen fromlist for this new list
NewList.remove(random.shuffle)
#to remove the word once randomised
NewList([18]*repetitions)
#repeat sequence above 18 times
file.write(newfilename.xlsx)

那有意义吗?请问有人知道如何帮助我吗?

任何帮助将非常感激!

谢谢你,凯特

4

2 回答 2

1

我认为你的想法是正确的。

要读取 excel 文件,可以使用 python 包 Pandas。

import pandas as pd
import random

data = pd.read_excel('file.xlsx','Sheet1') #Sheet1 name of the file
words = data['column_of_interest'].values #Select column containing words

random.shuffle(words)

一旦你的列表被洗牌,我认为你可以取 8 个第一个元素,然后再取 8 个,...

cpt = 0
chunks = {}
chunk = []
for e in range(len(list)):
    chunk.append(list[e])
    if len(chunk) == 8:
       chunks[cpt] = chunk
       cpt += 1
       chunk = []
if chunk != []:
    chunks[cpt] = chunk  # the last elements

你现在有一个大块的字典。您可以这样迭代(并在循环中写入新文件):

for i in chunks:
    chunk = chunks[i]
于 2014-05-21T09:59:02.987 回答
0

这应该让你开始:

import xlrd
import random
workbook = xlrd.open_workbook('test.xlsx') # open excel file

worksheet = workbook.sheet_by_name('Sheet1') 

words= [ str(x) for x in worksheet.col_values(colx=1) if x] # words in column 1

random.shuffle(words)

newList = [random.choice(words) for i in range(8) ] #8words to be randomly chosen fromlist for this new list

你需要看看一些 xls 阅读器和编写器xlswriterxlrd

于 2014-05-21T10:41:10.243 回答