用python中列表中的项目替换字符串中的一组字符
我有一个脚本,我正试图用python编辑它。简言之,目前它在某些平衡系统下将一组文件绑定在一起。我需要找到一个特定的关键字,并将其替换为随机列表中的一个项目。这是基本脚本,但没有以下功能:用python中列表中的项目替换字符串中的一组字符,python,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
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()
如果有任何帮助,我们将不胜感激。您有许多错误,
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])
您有很多错误,
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])
它显示了什么错误?当前显示的错误
回溯(最近一次调用):文件“C:\Backup\Test ATC August\ATCLab Condition Sorter Bullpup v1.6\generateScripts_1.6 Test.py”,第48行,str.datacopy=data.replace('blunt1.VBS',launchshuffled[0])TypeError:需要字符缓冲区对象
您从文件中读取数据,然后在使用它之前关闭它!把f.close()
放在末尾。我确实是通过将它定义为数据来使用它的,对吗?是的,我错了。将行launchshuffled=[Shuffle(launch)]
替换为launchshuffled=Shuffle(launch)
显示了什么错误?当前显示的错误Traceback(最近一次调用):文件“C:\Backup\Test ATC August\ATCLab Condition Sorter Bullpup v1.6\generateScripts_1.6 Test.py”,第48行,str.datacopy=data.replace('blunt1.VBS',launchshuffled[0])类型错误:需要一个字符缓冲区对象
您从文件中读取数据
,然后在使用它之前将其关闭!将f.close()
放在末尾。我是通过将它定义为数据来使用它的。是的,我的错误。将行launchshuffled=[Shuffle(launch)]
替换为launchshuffled=Shuffle(启动)
我明天将进行bug测试,然后选择\答案:)我明天将进行bug测试,然后选择\答案:)