2012-10-04 42 views
-1

搜索字符串与精确长度的文件我有文件包含如何使用python

google 
google talk browser 
google talks 
google talk 

如何使用Python搜索“谈判”的文件,准确地搜索,无需额外的字符串和使用python

打印线

回答

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 
+0

我想只搜索“交谈”,并打印行和不打印“谈话浏览器”和“会谈”我该怎么做。 – user1699309

+0

我很困惑。你不是在寻找一条等于'talk'的线吗? – Blender

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 
+0

此编码在我的系统中不工作 – user1699309

+0

已更新的回答。 – vivek

+0

对不起,我现在更新这是非常迫切的需要...请帮助 – user1699309