在循环中搜索和放入数组时遇到问题
Having Trouble Searching and Putting into Array During a Loop
所以我有一个 output.txt 文件,我要循环查找这一行“probability= 7.5098503e-03”,只要找到它,就将该概率值放入数组中。
我认为我在行中搜索 "probability" 时出现以下错误:
File "test.py", line 29, in search
if probability in line:
NameError: global name 'probability' is not defined
我也不确定我将概率放入数组中每个循环是否都能正常工作...
prob=[]
voltages = [0.3, 0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5]
for voltage in voltages:
update_code(voltage)
os.system("hspice pbit.sp >output.txt")
def search():
with open('output.txt') as f:
datafile = f.readlines()
for line in datafile:
if probability in line:
return True
return False
if search():
print('True')
prob=[] = line
else:
print('False')
我希望得到这个很棒的知识社区的帮助!
您需要添加引号才能成为字符串文字
if 'probability' in line:
否则该行会期望 probability
是一个变量
所以我有一个 output.txt 文件,我要循环查找这一行“probability= 7.5098503e-03”,只要找到它,就将该概率值放入数组中。
我认为我在行中搜索 "probability" 时出现以下错误:
File "test.py", line 29, in search
if probability in line:
NameError: global name 'probability' is not defined
我也不确定我将概率放入数组中每个循环是否都能正常工作...
prob=[]
voltages = [0.3, 0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5]
for voltage in voltages:
update_code(voltage)
os.system("hspice pbit.sp >output.txt")
def search():
with open('output.txt') as f:
datafile = f.readlines()
for line in datafile:
if probability in line:
return True
return False
if search():
print('True')
prob=[] = line
else:
print('False')
我希望得到这个很棒的知识社区的帮助!
您需要添加引号才能成为字符串文字
if 'probability' in line:
否则该行会期望 probability
是一个变量