2012-10-04 20 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