简体   繁体   English

有关编辑python代码的建议

[英]Advice with editing python code

I am creating a neuropsychological task using PsychoPy which uses Python code. 我正在使用使用Python代码的PsychoPy创建神经心理学任务。 I am creating a GoNoGo task and have a list of stimuli (positive, negative, and neutral words). 我正在创建一个GoNoGo任务,并且有一个刺激列表(正,负和中性词)。 I have a set number of blocks (18) and the target and distractor valence of the words are fixed within each of these blocks (ie., block 1 = target: Positive, distractor: Neutral etc.). 我有一定数量的块(18),单词的目标价和干扰价固定在这些块的每一个内(即,块1 =目标:正数,干扰字:中性等)。 Within each block, there are 8 target and 8 distractor words that appear in a random order (without replacement). 在每个块中,有8个目标词和8个干扰词以随机顺序出现(无替换)。

So, I want psychopy to randomly select 16 words from the relavant valence of the target (8 words) and distractor (8 words) for each block. 因此,我希望心理医生从目标的相对价位中随机选择16个单词(8个单词),并为每个块选择干扰因素(8个单词)。

I'm not experienced with code, but wondered if anyone would be able to help me with this particular one. 我没有代码方面的经验,但想知道是否有人可以帮助我解决这一特定问题。

To get psychopy to randomly choose words per block, I want a code that asks psychopy to read the excel file from a specific location, shuffle randomly the words which are in one of the columns in this file, split all those words in that column into chunks of 8 words, then to repeat this 18 times, and finally write these new chunks of 8 words to a new file. 为了使心理医生能够在每个块中随机选择单词,我想要一个代码,要求心理医生从特定位置读取excel文件,随机混排此文件中某一列中的单词,将该列中的所有这些单词拆分为8个单词的块,然后重复此18次,最后将这些8个单词的新块写入一个新文件。

I want a code that asks the program to read an excel file from a specific location, shuffle randomly the words which are in one of the columns in this file, split all those words in that column into chunks of 8 words, then to repeat this 18 times, and finally write these new chunks of 8 words to a new file. 我想要一个代码,要求程序从特定位置读取一个excel文件,随机混排此文件中某一列中的单词,将该列中的所有单词拆分为8个单词的块,然后重复此操作18次,最后将这些8个单词的新块写入一个新文件。

This is my code so far: 到目前为止,这是我的代码:

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)

Does that make sense? 那有意义吗? Would anybody know how to help me with this please? 有人知道如何帮助我吗?

Any help would be much appreciated! 任何帮助将非常感激!

Thank you, Kate 谢谢凯特

I think you have the correct idea. 我认为您的想法正确。

To read the excel file, you can use the python package Pandas. 要读取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)

Once your list if shuffled, I think you can take the 8 first element, then again 8, ... 一旦您的列表被改组,我认为您可以采用第一个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

You have now a dict of chunks. 您现在有了一大堆字典。 You can iterate like that (and write to a new file in the loop): 您可以像这样进行迭代(并在循环中写入新文件):

for i in chunks:
    chunk = chunks[i]

This should get you started: 这应该使您开始:

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

You need to take a look at some xls readers and writers xlswriter and xlrd 您需要看一下一些xls读者和作家xlswriterxlrd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM