Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python文件处理搜索精确字符串_Python - Fatal编程技术网

如何使用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