简体   繁体   中英

Advice with editing python code

I am creating a neuropsychological task using PsychoPy which uses Python code. I am creating a GoNoGo task and have a list of stimuli (positive, negative, and neutral words). 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.). Within each block, there are 8 target and 8 distractor words that appear in a random order (without replacement).

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.

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.

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.

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.

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, ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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