简体   繁体   中英

Having Trouble Searching and Putting into Array During a Loop

So I have an output.txt file I'm looking to loop through looking for this line " probability= 7.5098503e-03 " and whenever it is found, put that probability value into an array.

I think my searching for "probability" in line isn't working as I get this error:

File "test.py", line 29, in search

if probability in line:

NameError: global name 'probability' is not defined

I'm also not sure my putting the probabilities into an array every loop will work properly either...

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

I would love assistance from this awesome intellectual community!

You need to add quotes for that to be a string literal

if 'probability' in line:

Otherwise that line would expect probability is a variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM