str.endwith() return 在有效检查中为 false
str.endwith() return false in valid check
我不需要替代解决方案。
我正在使用 Python 2.5.4,想知道为什么会这样。
我为 makefile 编写源代码解析器。
ff = open("module.mk")
f = ff.readlines()
ff.close()
for i in f:
if ".o \" in i[-5:]:
print "Is %s for str: %s" %(i.endswith('.o \'), i)
我得到了:
Is False for str: bitmap.o \
每张支票也是如此。
你可以从github
得到module.mk
当您使用 .readlines()
时,它在行中包含换行符,在本例中为 CR-LF。
您需要在检查 .endswith()
之前删除该换行符:
with open("module.mk") as data:
for i in data.readlines():
if ".o \" in i[-5:]:
print "Is %s for str: %s" %(i.strip().endswith('.o \'), i)
注意:这里不需要 .readlines()
调用,我只是保留它以便行为与 OP 的代码相同。
我不需要替代解决方案。
我正在使用 Python 2.5.4,想知道为什么会这样。
我为 makefile 编写源代码解析器。
ff = open("module.mk")
f = ff.readlines()
ff.close()
for i in f:
if ".o \" in i[-5:]:
print "Is %s for str: %s" %(i.endswith('.o \'), i)
我得到了:
Is False for str: bitmap.o \
每张支票也是如此。
你可以从github
得到module.mk当您使用 .readlines()
时,它在行中包含换行符,在本例中为 CR-LF。
您需要在检查 .endswith()
之前删除该换行符:
with open("module.mk") as data:
for i in data.readlines():
if ".o \" in i[-5:]:
print "Is %s for str: %s" %(i.strip().endswith('.o \'), i)
注意:这里不需要 .readlines()
调用,我只是保留它以便行为与 OP 的代码相同。