Python Array, trying to place text file into an array -


i trying complete piece of coursework need place text file full of key words array when type error message saying:

typeerror: 'in ' requires string left operand, not list

what mean?

here code:

#!/usr/bin/python  array = [] open("test.txt", "r") f:   line in f:     array.append(line) print array  searchfile =open('test.txt','r') line in searchfile:     if array in line: print line searchfile.close() 

edit: thank reply have changed line display this:

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

the search works except have keywords document containing simple words 'green, blue etc' (all on own line) have document text such 'my shirt green' when use code wont find if change sentence word find it. need search document keywords , display whole line in.

it means you're attempting check whether list in string, line:

if array in line: 

my guess you're attempting check if line in array, in statement must reversed:

if line in array: 

so aren't confused errors in future, note in operator. left operand whatever object left of operator part of in operation. there, it's pretty clear if @ types error indicating.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -