2016-07-31 73 views
-1

我正在嘗試編寫一個程序,該程序讀取文件的內容,逐行搜索文件,並在出現某些字符串時將它返回給我的屏幕。打印文件中出現的字線

我已經成功,只有一個字符串,我正在尋找。我試圖現在,當我告訴Python找了幾個字符串,當他們中的一個文件中出現,它打印我行

import urllib2 
file = urllib2.urlopen("http://helloworldbook2.com/data/message.txt") 
message = file.read() 
#print message 

# Saves the content of the URL in a file 
temp_output = open("tempfile.txt", "w") 
temp_output.write(message) 
temp_output.close() 

# Displays the line where a certain word occurs 
wordline = [line for line in message.split('\n') if ("enjoying" or "internet") in line] 
line = wordline[0] 
print line 

所以我的問題是(「享受」或「互聯網」)的一部分。

謝謝!

+0

[這個答案](http://stackoverflow.com/a/3271485/3697202 )可能有你正在尋找的解決方案。 –

+0

是的,這實際上解決了我的問題!謝謝! – Khaled

回答

0

這句法是沒有好:

if ("enjoying" or "internet") 

相反,你可以使用anyall

words = ["enjoying", "internet"] 
if all(x in line for x in words)