使用Python中的列表中的一个项目替换字符串中的一组字符

2024-04-27 21:14:13 发布

您现在位置: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]处添加一个项 到目前为止,我的解决方案失败得很惨,我也不知道为什么

    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()

任何帮助都将不胜感激。你知道吗


Tags: ofthetoinforreaddatagroup
1条回答
网友
1楼 · 发布于 2024-04-27 21:14:13

你有很多错误,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])

相关问题 更多 >