Python Array trying to place a text file into an array

I am trying to complete part of the coursework, so you need to put a text file full of keywords in an array, when I do that I get an error like:

TypeError: 'in' requires string as left operand, not a list

What does it mean?

Here is my code:

#!/usr/bin/python

array = []
with open("test.txt", "r") as f:
  for line in f:
    array.append(line)
print array

searchfile =open('test.txt','r')
for line in searchfile:
    if array in line: print line
searchfile.close()

      

EDIT: Thanks for the answer. I changed the line to reflect this:

searchfile =open('test.txt','r')
for line in searchfile:
    if line in array: print line
searchfile.close() 

      

Search work, except that I have a keyword document containing simple words like "green, blue, etc." (all on their own line), I have a document with text like "my shirt is green" when I use this code. you won't find anything, but if I change the sentence to one word, it finds it. I need to search for a document by keywords and then display the entire line it was in.

+3


source to share


2 answers


This means that you are trying to check if the list is on a string with this line:

if array in line:

      

I assume you are trying to check if a string is in array

, so your statement in

should be canceled:



if line in array:

      

Lest you get confused by such errors in the future, please note what in

is operator . The left operand is any object that is to the left of your operator that is part of an operation in

. From there it is pretty clear if you look at the types what the error indicates.

+3


source


#!/usr/bin/python

array = []
with open("test.txt", "r") as f:
  for line in f:
    array.append(line)
print array

searchfile =open('test.txt','r')
for line in searchfile:
    if line in array: print line
searchfile.close()

      

Now we need to work.

if line in array:

      

Is the changed string now searches array

for line

, rather than checking if the array is in the string (it won't).

EDIT: Forgot this bit ... The error is that you are looking for a string for a list and this will throw an error as the list cannot be contained in the string. TypeError is raised when the operation is applied to the wrong type. This error is python saying you cannot search for a string for a list.

EDIT 2 In response to editing, you need to split the lines of the file with a space " "

and then loop over it.

array = []
with open("words.txt", "r") as f:
    for line in f:
        array.append(line)
        print array

searchfile =open('sent.txt','r')
for line in searchfile:
    sLine = line.split(" ")
    for word in sLine:
        if word in array:
            print line
searchfile.close()

      

Now it iterates over each line in the file and then splits and stores the line as a new list - sLine

. Then each element of this list is checked for array

.



However, there is a mistake in this: if the search word is not at the end of the line, it will not be found, since each element in array

contains \n

, but some searchfile

cannot.

array = []
with open("words.txt", "r") as f:
    for line in f:
    line = line.rstrip("\n")
    array.append(line)
print array

searchfile =open('sent.txt','r')
for line in searchfile:
    sLine = line.split(" ")
    for word in sLine:
        word = word.rstrip("\n")
        if word in array:
            print line,
searchfile.close()

      

This word will now be found even if it is embedded in a sentence.

eg.

array - cat

searchfile - the cat was sitting on the rug

cat will now be found even if it is in the sentence.

The comma at the end of the print should stop if there is a line between each printed line.

-1


source







All Articles