我有多个带有名称和统计信息的文件以及一个检查某个统计信息的文件。
Example File 1
Eddy,23,2,4,9,AB
Frank,46,2,4,5,DA
Example File 2
AB
B
BA
DA
DH
我没有收到任何错误,但它没有写入新文件。
我用来执行此操作的代码是:
# Open Removal file and create a set of required removals
book_removals = open("File2.csv")
search_removals_col = 0
# Open the All Candidates file
book_candidates = open('File1.csv')
search_candidates_col = 4
# Create a New Roster file
book_new = open('upload.csv')
# Iterate through candidates file, looking for removals
for row in range(search_candidates_col):
if book_candidates == book_removals:
book_new.write(row)
book_new.flush
book_new.close()
答案 0 :(得分:0)
import csv
stats = set()
with open('File2.csv') as file2:
for line in file2:
stats.add(line)
with open('File1.csv', newline='') as file1:
file1_reader = csv.reader(file1)
with open('output.csv', 'w+', newline='') as output:
output_writer = csv.writer(output)
for line in file1_reader:
if any(item in stats for item in line):
output_writer.writerow(line)
处理.csv文件时,请使用csv
模块。这会在set
中构建File2
行,然后遍历File1
中的每一行。如果该行的值为File2
,则该行将写入output.csv