简体   繁体   中英

Python search file and print line if matches anything in array

I have an array with multiple strings in it. I need to search through a file to see if any line in the file matches with any of the strings in the array and print all the lines from the file that matches

this is what i have so far, but my python syntax/logic is a little off

under30=[] is the array of multiple strings i want to match against the file 
with open("list.txt") as f2:
        for line in f2:
                if under30() in line:
                        print line

Assuming that under30 is your list of string to match from the file

if under30() in line:

should be:

if line in under30:

You can try this:

file_data = [i.strip('\n') for i in open('filename.txt')]
under30=["string1", "string2", "string3"]
final_lines = [i for i in file_data if i in under30]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM