该数组有许多项,然后在python中循环一定次数


['LED_ON', 'Dummy', 'LED_INTENSITY']

我有一个数组,里面有固定数量的项目,现在有3个,但可能或多或少。然后我想通过循环设置的次数来运行一些东西,数组中有3个项目,所以循环3次

如何获得阵列

subtest=[]
with open(st,'r') as f:
for line in f:
if line.startswith("   subtest"):
subtest.append(line[12:-2])

f.close()
print(subtest)

这是我的文件

functional
subtest "LED_ON"
wire "PSOC_HB_R"    to 204152 aux alternate
wire "PWR_PSOC_LED" to 205152 source
wire "LED1-OUT-1"   to 216103 detector high
wire "SENSOR-GND"   to  21606 detector low
end subtest
subtest "Dummy"
wire "PWR_PSOC_LED" to 205152 aux
wire "PSOC_HB_R"    to 204152 source alternate
end subtest
subtest "LED_INTENSITY"
wire "PSOC_HB_R" to 204152 aux alternate
wire "PWR_PSOC_LED" to 205152 source
wire "LED1-INTENSITY-1" to  21603 detector high
wire "SENSOR-GND" to  21606 detector low
end subtest

对于环路

import re
#get data from wirelist.dat 
src_map = {}
in_test = False
with open(st,'r') as f:
for line in f:
if (line.strip() == '') : 
#print ('blank line')
continue
if (not in_test) and not re.match("s+subtests+""+info+""",line): # skip
print ('not in test, skip')
continue
elif re.match("s+subtests+""+info+""",line):
print ('found subtest start')
in_test = True
continue
elif (in_test) and ("end subtest" in line):
print ('found subtest end')
in_test = False
break
elif not (in_test):
print ('not in test')
continue
else: # must be good line in LED subtest
print ('good line')
m = re.match(r"s+wires+("[^"]+")s+tos+(d+)s+(aux alternate|aux|source|source alternate|detector high|detector low).*", line)
if not m: # not a line we are interested in
continue
else:
print (m)
term1 = m.group(1)
term2 = m.group(2)
term3 = m.group(3)
if term3.startswith('aux'):
src_map ['a'] = term1
elif term3 == 'source' :
src_map ['s'] = term1
elif term3.endswith('high'):
src_map ['i'] = term1
else:
src_map['l'] = term1
print(src_map)
#find the data that want to be replace
in_test = False
lines_out = []
with open(file,'r') as input_file:
for line in input_file:
#print (line)
if (line.strip() == '') : 
#print ('blank line')
continue
if (not in_test) and not re.match("subtests+""+info+""",line): # skip
print ('not in test, skip')
lines_out.append(line)
elif re.match("subtests+""+info+""",line):
print ('found subtest start')
in_test = True
lines_out.append(line)
elif (in_test) and ("end subtest" in line):
print ('found subtest end')
in_test = False
lines_out.append(line)
elif not (in_test):
print ('not in test')
lines_out.append(line)
else: # must be good line in LED subtest
print ('good line')
m = re.match(r"s+connects+(w)s+tos+pinss+(d).*", line)
if not m: # not a line we are interested in
lines_out.append(line)
else:
print (m)
term1 = m.group(1)
term2 = src_map[term1]
#map to wire values using connection type

new_line = f"   connect {term1} to nodes {term2}n"
lines_out.append(new_line)
#replace data                
with open('mynewfile.dat','a') as outfile:
outfile.writelines(lines_out)
outfile.close()

在我的循环中有info,我想用数组中的项替换它。例如,第一个循环将是子测试[0],第二个循环将为子测试[1],依此类推

如果你能给我一些想法,那就太好了,因为我已经被困在这里三天了,仍然没有结果,而且我从来没有见过有人使用这种循环(嗯,我只有开始编程和学习编程3周,所以请帮助我(

您可以在循环中运行循环:

import re
#get data from wirelist.dat 
src_map = {}
in_test = False
with open(st,'r') as f:
for line in f:
for st in subtest:
if (line.strip() == '') : 
#print ('blank line')
continue
if (not in_test) and not re.match("s+subtests+""+st+""",line): # skip
print ('not in test, skip')
continue
elif re.match("s+subtests+""+st+""",line):
print ('found subtest start')
in_test = True
continue
elif (in_test) and ("end subtest" in line):
print ('found subtest end')
in_test = False
break
elif not (in_test):
print ('not in test')
continue
else: # must be good line in LED subtest
print ('good line')
m = re.match(r"s+wires+("[^"]+")s+tos+(d+)s+(aux alternate|aux|source|source alternate|detector high|detector low).*", line)
if not m: # not a line we are interested in
continue
else:
print (m)
term1 = m.group(1)
term2 = m.group(2)
term3 = m.group(3)
if term3.startswith('aux'):
src_map ['a'] = term1
elif term3 == 'source' :
src_map ['s'] = term1
elif term3.endswith('high'):
src_map ['i'] = term1
else:
src_map['l'] = term1
print(src_map)

有人帮我解决了reddit 的这个问题

file=input('Enter file:')
f=file.split(".")
w=(f[0])
st=('st_' + w + '.dat')
from collections import defaultdict
src_map = defaultdict(dict)
in_test = False
subtest = None
with open(st,'r') as f:
for line in f:
if (line.strip() == '') : 
#print ('blank line')
continue
if (not in_test) and not line.startswith("   subtest"): # skip
print ('not in test, skip')
continue
elif re.match("s+subtests+"([^"]+)"",line):
m = re.match("s+subtests+"([^"]+)"",line)
subtest = m.group(1)
print (f'found subtest {subtest} start')
in_test = True
continue
elif (in_test) and ("end subtest" in line):
print (f'found subtest {subtest} end')
in_test = False
subtest = None
continue
elif not (in_test):
print ('not in test')
continue
else: # must be good line in LED subtest
print ('good line')
m = re.match(r"s+wires+("[^"]+")s+tos+(d+)s+(aux alternate|aux|source|source alternate|detector high|detector low).*", line)
if not m: # not a line we're interested in
continue
else:
print (m)
term1 = m.group(1)
term2 = m.group(2)
term3 = m.group(3)
if term3.startswith('aux'):
src_map [subtest]['a'] = term1
elif term3 == 'source' :
src_map [subtest]['s'] = term1
elif term3.endswith('high'):
src_map [subtest]['i'] = term1
else:
src_map [subtest]['l'] = term1
print(src_map)

#find the data that want to be replace
in_test = False
lines_out = []
with open(file,'r') as input_file:
for line in input_file:
#print (line)
if (line.strip() == '') : 
#print ('blank line')
continue        
if (not in_test) and not line.startswith('subtest'): # skip
print ('not in test, skip')
lines_out.append(line)
elif re.match("subtests+"([^"]+)"",line):
m = re.match("subtests+"([^"]+)"",line)
subtest = m.group(1)
print (f'found subtest {subtest} start')
in_test = True
lines_out.append(line)
elif (in_test) and ("end subtest" in line):
print (f'found subtest {subtest} end')
in_test = False
lines_out.append(line)
elif not (in_test):
print ('not in test')
lines_out.append(line)
else: # must be good line in LED subtest
print ('good line')
m = re.match(r"s+connects+(w)s+tos+pinss+(d).*", line)
if not m: # not a line we're interested in
lines_out.append(line)
else:
print (m)
term1 = m.group(1)

term2 = src_map[subtest][term1]
new_line = f"   connect {term1} to nodes {term2}n"
lines_out.append(new_line)

with open('mynewfile.dat','a') as outfile:
outfile.writelines(lines_out)
outfile.close()
os.remove(st)
os.remove(file)
os.rename("mynewfile.dat",file)

最新更新