我有包含几行文本的文件。我在我的代码中以这种方式打开文件:
final = codecs.open("info", 'r', 'utf-8')
lines = final.readlines()
然后我想找到确切的行:
if 'Edytuj'.join('\n') in lines:
#rest of code
我知道该文件包含'Edytuj'
但该语句始终False
是,我在某处犯了错误吗?
你看过什么'Edytuj'.join('\n')
回报吗?(它会返回'\n'
,因为您要加入长度为 1 的可迭代对象)。我猜这不是你的本意。
您究竟希望该测试做什么?
也许你的意思是:
if any('Edytuj' in line for line in lines):
pass #...
或者可能:
if 'Edytuj\n' in lines:
with codecs.open("info", 'r', 'utf-8') as file:
for line in file:
if u'Edytuj\n' == line: # match full line
#rest of code
if u'Edytuj' in line
如果该行中可能有其他内容,您可以使用。