-1

I am trying to write a program that reads the content of a file, searches the file line by line and when certain strings occur, it returns me that line to my screen.

I have succeeded when it is only ONE string that I am looking for. What I am trying now is, when I tell Python to look for a number of strings and when one of them occurs in the file, it prints me that line

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

So my problem is the ("enjoying" or "internet") part.

Thanks!

Kai
  • 299
  • 6
  • 13

1 Answers1

0

This syntax is no good:

if ("enjoying" or "internet")

Instead, you can use any or all:

words = ["enjoying", "internet"]
if all(x in line for x in words)
Idos
  • 15,053
  • 14
  • 60
  • 75