使用python

时间:2015-08-17 09:29:30

标签: python

我正在尝试在python中编辑脚本。简而言之,目前它在一些平衡系统下将一堆文件连接在一起。我需要在列表中随机化的项目中找到并替换所有这些关键字中的某个关键字。这是没有这些功能的基本脚本,可以在下面正常工作:

import random

# General comment: some of the script might be confusing because python
# uses zero-based numbering to index arrays

# read in the full list of scenario x conditions
f = open('scenarioList.xml', 'U')
data = f.read()
f.close()
inst = data.split("\n\n")

# read in the main portion of the data file (all before scenariolist)
f = open('interruptionstest1.xml', 'U')
MainScript = f.read()
f.close()

# read the final few lines at the end of the file
f = open('interruptionstest2.xml', 'U')
EndScript = f.read()
f.close()

# This specifies which scenarios are in which counterbalancing group
cGroups = [[0,3,6,9,12],
[1,4,7,10,13],
[2,5,8,11,14]]

conds = [inst[0:15],inst[15:30],inst[30:45]] # the xml strings divided up by condition

# this is the counterbalancing scheme (latin square)
cScheme = [[1,2,3],
[1,3,2],
[2 ,1 , 3],
[2 ,  3 ,  1],
[3 ,  1  , 2],
[3,   2 ,  1]]

# change the second index in the range to loop up to 60; set to 12 now for testing
for subj in range(1,61): 

    cRow = cScheme[(subj-1)%6] # use the modulus operator to find out which row to use in counterbalancing table

    scenArray = []

    # loop across scenario groups and look up their assigned interruption condition for this subj
    for group in range(0,3):
        #conds[cScheme[group]][i]

        scenArray.extend([ conds[cRow[group]-1][i] for i in cGroups[group]]) # use extend and not append here

    # randomize order of arrays---this is something you might modify to control this a bit more
    random.shuffle(scenArray)

    #insert workload and rest breaks with the exception of 8 which is a long rest
    testingArray = sum(([x,'\t\t\t<atc:instruction atc:idxref="Workload1"/>\n\t\t\t<atc:instruction atc:idxref="Anxiety1"/>\n\t\t\t<atc:instruction atc:idxref="Rest"/>'] if i != 7 else [x,'\t\t\t<atc:instruction atc:idxref="Workload1"/>\n\t\t\t<atc:instruction atc:idxref="Anxiety1"/>\n\t\t\t<atc:instruction atc:idxref="Restlong"/>'] for (i,x) in enumerate(scenArray)), [])

    f = open('ATC_Golf_Participant' + str(subj) + '.' + 'xml', 'w')
    f.write(MainScript + '\r\n\r\n'.join(testingArray) + '\n' + EndScript)
    f.close()

我需要什么:当脚本循环for subj in range(1,61):时,我需要它将blunt1.VBS这个词替换为blunt5.VBS,索引为[0-4] 洗牌清单launch = ['launch1.VBS','launch2.VBS','launch3.VBS','launch4.VBS','launch5.VBS'] 到目前为止,我的解决办法惨遭失败,我无法解决为什么

    import random

# General comment: some of the script might be confusing because python
# uses zero-based numbering to index arrays

# read in the full list of scenario x conditions
f = open('scenarioList.xml', 'U')
data = f.read()
f.close()

#defines function shuffle
def Shuffle(x):
    b = x[:]#copies the launch commands
    random.shuffle(b)#shuffles the copy
    return b

#Specifies the ATC interruption launch commands
launch = ['launch1.VBS','launch2.VBS','launch3.VBS','launch4.VBS','launch5.VBS']

# read in the main portion of the data file (all before scenariolist)
f = open('interruptionstest1.xml', 'U')
MainScript = f.read()
f.close()

# read the final few lines at the end of the file
f = open('interruptionstest2.xml', 'U')
EndScript = f.read()
f.close()

# This specifies which scenarios are in which counterbalancing group
cGroups = [[0,3,6,9,12],
[1,4,7,10,13],
[2,5,8,11,14]]

# this is the counterbalancing scheme (latin square)
cScheme = [[1,2,3],
[1,3,2],
[2 ,1 , 3],
[2 ,  3 ,  1],
[3 ,  1  , 2],
[3,   2 ,  1]]

# change the second index in the range to loop up to 60; set to 12 now for testing
for subj in range(1,61):

    launchshuffled = [Shuffle(launch)]          
    datacopy = data.replace('blunt1.VBS',launchshuffled[0])
    datacopy = data.replace('blunt2.VBS',launchshuffled[1])
    datacopy = data.replace('blunt3.VBS',launchshuffled[2])
    datacopy = data.replace('blunt4.VBS',launchshuffled[3])
    datacopy = data.replace('blunt5.VBS',launchshuffled[4])

    inst = datacopy.split("\n\n") #split the data file
    conds = [inst[0:15],inst[15:30],inst[30:45]] # the xml strings divided up by condition

    cRow = cScheme[(subj-1)%6] # use the modulus operator to find out which row to use in counterbalancing table

    scenArray = []

    # loop across scenario groups and look up their assigned interruption condition for this subj
    for group in range(0,3):
        #conds[cScheme[group]][i]

        scenArray.extend([ conds[cRow[group]-1][i] for i in cGroups[group]]) # use extend and not append here

    # randomize order of arrays---this is something you might modify to control this a bit more
    random.shuffle(scenArray)

    #insert workload and rest breaks with the exception of 8 which is a long rest
    testingArray = sum(([x,'\t\t\t<atc:instruction atc:idxref="Workload1"/>\n\t\t\t<atc:instruction atc:idxref="Anxiety1"/>\n\t\t\t<atc:instruction atc:idxref="Rest"/>'] if i != 7 else [x,'\t\t\t<atc:instruction atc:idxref="Workload1"/>\n\t\t\t<atc:instruction atc:idxref="Anxiety1"/>\n\t\t\t<atc:instruction atc:idxref="Restlong"/>'] for (i,x) in enumerate(scenArray)), [])

    f = open('ATC_Golf_Participant' + str(subj) + '.' + 'xml', 'w')
    f.write(MainScript + '\r\n\r\n'.join(testingArray) + '\n' + EndScript)
    f.close()

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您有很多错误,Shuffle(launch)已经是一个列表,您不需要添加[]。您还应该使用datacopy进行替换,因为string.replace不会就地更改字符串。

替换:

launchshuffled = Shuffle(launch)          
datacopy = data.replace('blunt1.VBS',launchshuffled[0])
datacopy = datacopy.replace('blunt2.VBS',launchshuffled[1])
datacopy = datacopy.replace('blunt3.VBS',launchshuffled[2])
datacopy = datacopy.replace('blunt4.VBS',launchshuffled[3])
datacopy = datacopy.replace('blunt5.VBS',launchshuffled[4])