简体   繁体   中英

Any ideas why this code won't work

I have multiple files with names and stats and a file that checks for a certain stat.

Example File 1
Eddy,23,2,4,9,AB
Frank,46,2,4,5,DA

Example File 2
AB
B
BA
DA
DH

I am not getting any errors but it is not writing to the new file.

The code I am using to do this is:

# 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()
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)

When dealing with .csv files, use the csv module. This builds a set out of lines in File2 , then goes through every line in File1 . If that line has a value from File2 , then that line is written to output.csv

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