如何使用python文件处理搜索精确字符串
我有一个包含如何使用python文件处理搜索精确字符串,python,Python,我有一个包含 "1111 1111 1111 // google 1111 1111 1111 // google talk browser 1111 1111 1111 // google talks 1111 1111 1111 // google talk" 我只想打印“//google talk”相关行(仅第4行) 像这样试过,但不起作用 with open('file.txt', 'r') as handle: for index, line in enumerate
"1111 1111 1111 // google
1111 1111 1111 // google talk browser
1111 1111 1111 // google talks
1111 1111 1111 // google talk"
我只想打印“//google talk”相关行(仅第4行)
像这样试过,但不起作用
with open('file.txt', 'r') as handle:
for index, line in enumerate(handle, 1):
if line.strip() == 'talk':
print 'Match found on line', index
你所说的“无需额外字符串即可精确搜索”是什么意思?在本例中,您希望的输出是什么?欢迎使用堆栈溢出!我们鼓励你这样做。如果您有,请将其添加到问题中-如果没有,请先研究并尝试您的问题,然后再回来。是的,我编辑了,请帮助回答上述问题
with open('file.txt', 'r') as handle:
for index, line in enumerate(handle, 1):
if line.rstrip().endswith("// google talk") # or .endswith("talk")
print 'Match found on line', index