如果匹配文本,请写行

时间:2016-12-21 16:16:12

标签: python csv

我正在尝试获取一个单词列表,并且对于单词列表中的每个关键字,我想搜索它是否在单独文件中的一行上。如果关键字在一行中,我想写那一行。我遇到的问题是我的代码是写每一行而不只是有关键字的行。这就是我所拥有的:

import csv

open_output = open('list.csv','wb')
output_file = csv.writer(open_output)

searchedfile = open("searchedfile.csv",'rb')
read_file = csv.reader(searchedfile)

f = open('wordlist.csv')
csv_f = csv.reader(f)
words = []
for row in csv_f:
    item = row[0]
    words.append(item)

for line in read_file:
    if any(word in line[5] for word in words):
        output_file2.writerow([line[5]])

0 个答案:

没有答案