Python - 搜索一个单词,但两个相似,所以我得到两个

时间:2017-07-03 13:02:33

标签: python

在.log文件中搜索关键字,在本文档中是我想稍后排序的测试字符串,但我的问题是我有两个不同的字符串,其中包含关键字...我的搜索关键字是“Tester” “但是另一个字符串有”发送请求:测试器“,所以当我打印我的关键字时我得到两个字符串...如何用”发送请求:测试器“排除字符串并且只打印”测试器“字符串?我已经尝试了if not, or, and, not, and others但是无法得到我想要的东西...请帮忙,是否有某种类型的排除功能?

修改

if 'Tester' in lines:
data.append(lines)
data.remove('Sending Request: Tester')
print(data) # wont work...

if 'Tester' == lines:
data.append(lines)
print(data) # wont work

这是文件的样子,我想只提取测试器字符串“Tester - >”数字不是“发送请求:测试人员 - >” 号码..

    Sending Request: Tester -> 12341244
    Tester ->12341244
    Sending Request: Tester ->234314
    Tester ->234314
    Sending Request: Tester ->12342314
    Tester ->12342314

1 个答案:

答案 0 :(得分:0)

尝试使用函数startswith

if lines.startswith('Tester'):  

https://www.tutorialspoint.com/python/string_startswith.htm