ViolList是一个包含元素['a230kv'、'bcd120kv(after)'、'yz ckt1']的列表,txt文件有如下行
说明“BCD 120kv(之后)
第1行
说明A 230kv-fg 115kV ckt 2
第2行
说明yz ckt 1
第3行
描述
行
我需要完全匹配它们,这样一个230kv!=230kv-fg 115kV ckt 2
with open(ContFile.get(), "r") as content_file:
sectionFound = False
for line in content_file:
for x in ViolList:
x = 'description ' + x
if x.upper() == line.upper():
sectionFound = True
print sectionFound
break
if sectionFound == True:
outfile.write('Found")
下图显示了“line”和“x”,但仍然没有返回“Found”:
我认为您的问题是,文件行中有时有您看不到或不需要的空间。
这是一种猜测,但你可以尝试strip你的台词,你永远不知道。
因此,要做到这一点,只需替换这个:
通过这个:
也可能是打字错误,但你写道:
最后,我只想指出,对于实际的代码,如果您找到一个正确的行元素匹配,它将不断地为列表中的其余元素编写找到的代码,如果您不想,还可以添加:
在写入outfile或替换现有文件之后:
线内迭代,或:
如果不想继续检查列表中的其他元素
最后一件事的例子:
希望对你有帮助
相关问题 更多 >
编程相关推荐