[英]Two strings and unicodes that are exactly the same do not return true when evaluated with each other
[英]Why do two of the same strings not return as being the same when compared?
我有以下代码:
file = open('AdjectivesList.txt', 'r')
lines = file.readlines()
file.close()
for word in words:
wordLowercase = word.lower()
for x, lol in enumerate(lines):
gg = (lines[x].lower())
if wordLowercase == gg:
print('identified')
即使wordLowercase
等于gg
,也不会打印字符串“ identified”。 为什么会这样呢?
.readlines()
在文本文件中每行的末尾包含换行符。 这很可能是您造成问题的原因。 您可以使用.strip()
删除换行符(以及字符串左右两侧的所有空白字符.strip()
。
gg = lines[x].lower().strip()
参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.