python - 数组有项目数,然后在 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+subtest\s+\""+info+"\"",line): # skip
print ('not in test, skip')
continue
elif re.match("\s+subtest\s+\""+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+wire\s+(\"[^\"]+\")\s+to\s+(\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("subtest\s+\""+info+"\"",line): # skip
print ('not in test, skip')
lines_out.append(line)
elif re.match("subtest\s+\""+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+connect\s+(\w)\s+to\s+pins\s+(\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
它,我想用我的数组中的项目替换它。例如,第一个循环是 subtest[0],第二个循环是 subtest[1],依此类推。
如果您能给我一些想法,那就太好了,因为我在这里被困了 3 天仍然没有结果,而且我从未见过有人使用这种循环(好吧,我才开始编程和学习编程 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+subtest\s+\""+st+"\"",line): # skip
print ('not in test, skip')
continue
elif re.match("\s+subtest\s+\""+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+wire\s+(\"[^\"]+\")\s+to\s+(\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)
推荐阅读
- ionic-framework - Ionic 3 图像未显示在设备上
- hibernate - 在多线程环境中与 Hibernate 会话进行交互的合适方式
- c# - 如何使用 SSIS 脚本任务删除 Excel 目标中的第二行?SQL Server 2012
- python - tkinter 顶级小部件的协议有哪些
- swift - 如何在 Promisekit 循环中使用 when 函数
- linux - scp 从 Linux 到 Windows: 'C:\Program' 无法识别错误
- json - 发送标志而不是基于 json 的字母
- entity-framework - MVC 业务逻辑 + DAL + 映射 ViewModel 和 EF 模型
- pdf - 使用 itext 7 和 GlobalSign 的文档签名服务 (DSS) 签署文档
- java - 系统找不到指定的文件试图创建 .jks 文件