-1

我有文件包含

google 
google talk browser
google talks
google talk

如何使用python在文件中搜索“talks”并在没有额外字符串的情况下精确搜索并使用python打印该行

4

2 回答 2

1

只需使用比较运算符:

with open('file.txt', 'r') as handle:
    for index, line in enumerate(handle, 1):
        if line.strip() == 'google talk':
            print 'Match found on line', index
于 2012-10-04T05:14:20.193 回答
0
import re

txt = open("youfile.txt").read()
if re.search(r'[^\s]?google talk[\s$]?',txt):
    //code for string present
else:
    //code for string not-present
于 2012-10-04T05:19:57.200 回答