如果与tex匹配,则写入行

2024-04-19 16:13:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图采取一个单词列表和每个关键字在单词列表中,我想搜索,如果它是在一行在一个单独的文件。如果关键字在一行中,我想写那行。我遇到的问题是,我的代码正在编写每一行,而不仅仅是有关键字的行。以下是我所拥有的:

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]])

Tags: csvin列表forreadoutputline关键字