gpt4 book ai didi

python - 无法比较python中的两个字符串

转载 作者:行者123 更新时间:2023-12-02 16:04:20 25 4
gpt4 key购买 nike

我需要检查是否有函数调用,我知道它不是 txt 文件,但是当我读取一行并尝试使用 type() 打印时,它说 str 并让我正确打印所有文件但是无论出于何种原因,无法比较这些行,我不知道为什么,当我编译时没有显示错误,文件的第一行是 '#include <Arduino.h>'而且找到的 var 无论如何都是假的

import os
def BeforeBuild():
found = False
with open(r"\src\OTAWEBAP.cpp", "r") as f:
for line in f:
print (line)
if(line == '#include <Arduino.h>'):
found = True;
if(not found):
raise Exception("OtaIni function not found, you need to use it to preserve OTA functions in your new deploy")
else:
print('function OtaIni was found')
f.close()
BeforeBuild()

最佳答案

尝试替换

if(line == '#include <Arduino.h>'):
found = True;

if line.strip() == '#include <Arduino.h>':
found = True

strip() 函数删除行首和行尾的所有空格。

P. S. 尽量记住,在 Python 中,if 条件不需要放在括号中,行末尾也不需要分号。否则每个人都会知道你本质上是一个 C 程序员。

关于python - 无法比较python中的两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69848805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com