无法比较 python 中的两个字符串
Can´t compare two strings in python
我需要检查是否有函数调用,我知道它不是 txt 文件,但是当我读取一行并尝试使用 type() 打印时,它说 str 并让我打印所有文件正确但出于某种原因无法比较这些行,我不知道为什么,当我编译时没有显示错误,文件的第一行是 '#include <Arduino.h>'
并且找到的 var 无论如何都是 false
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()
换行的话最后一个字符是\n
所以这可能对你有用:-
import os
def BeforeBuild():
found = False
with open(r"\src\OTAWEBAP.cpp", "r") as f:
for line in f:
print (line)
if(line[:-1] == '#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程序员。
比较字符串时要小心。在这种情况下,一个 whitespace 字符导致了这个问题。有很多 whitespace 字符看不到但可能会出现。因此,在处理此类文件时,一个好的做法是删除这些 whitespace 字符。
您可以使用 strip() 从字符串的两端删除 whitespace 个字符。
import os
def BeforeBuild():
found = False
with open(r"\src\OTAWEBAP.cpp", "r") as f:
for line in f:
line = line.strip();
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()
我需要检查是否有函数调用,我知道它不是 txt 文件,但是当我读取一行并尝试使用 type() 打印时,它说 str 并让我打印所有文件正确但出于某种原因无法比较这些行,我不知道为什么,当我编译时没有显示错误,文件的第一行是 '#include <Arduino.h>'
并且找到的 var 无论如何都是 false
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()
换行的话最后一个字符是\n
所以这可能对你有用:-
import os
def BeforeBuild():
found = False
with open(r"\src\OTAWEBAP.cpp", "r") as f:
for line in f:
print (line)
if(line[:-1] == '#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程序员。
比较字符串时要小心。在这种情况下,一个 whitespace 字符导致了这个问题。有很多 whitespace 字符看不到但可能会出现。因此,在处理此类文件时,一个好的做法是删除这些 whitespace 字符。 您可以使用 strip() 从字符串的两端删除 whitespace 个字符。
import os
def BeforeBuild():
found = False
with open(r"\src\OTAWEBAP.cpp", "r") as f:
for line in f:
line = line.strip();
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()