python - Finding lines that include two or more words from a list -
i have written code checks lines in infile matches of keywords in keyword file. now, want instead find lines include 2 or more of keywords in combination.
infile = open('/path/#input.txt', 'r') outfile = open('/path/#output.txt', 'w') # read textfile containing keywords find # (and strip newline character '\n') keywords = [line.strip() line in open('path/#keywords.txt')] # see lines in infile match of keywords # , write lines outfile line in infile: if all(k in line k in keywords): outfile.write(line)
here 1 way
if len(set(line.split()).intersection(keywords)) > 2:
this splits line words line.split()
first . uses sets intersection function find mutual elements of 2 sets
python
No comments:
Post a Comment