简体   繁体   English

在循环期间无法搜索和放入数组

[英]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.所以我有一个 output.txt 文件,我正在寻找循环查找这一行“probability= 7.5098503e-03”,每当找到它时,将该概率值放入一个数组中。

I think my searching for "probability" in line isn't working as I get this error:我认为我在行中搜索“概率”不起作用,因为我收到了这个错误:

File "test.py", line 29, in search搜索中的文件“test.py”,第 29 行

if probability in line:如果概率符合:

NameError: global name 'probability' is not defined NameError:未定义全局名称“概率”

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否则,该行将期望probability是一个变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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