繁体   English   中英

为什么两个相同的字符串在比较时不返回相同的字符串?

[英]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.

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