import csv import sys def filter_csv(input_csv, columns_file, output_csv): # Read the columns of interest from the text file with open(columns_file, 'r') as f: columns_of_interest = [line.strip() for line in f] # Read the input CSV file and filter based on the columns of interest with open(input_csv, 'r') as f: reader = csv.DictReader(f) # Read the CSV as a dictionary # Identify which columns exist in the original CSV all_columns = reader.fieldnames columns_to_keep = ['id'] + [col for col in columns_of_interest if col in all_columns] # Prepare to write the filtered data to a new CSV with open(output_csv, 'w', newline='') as out_f: writer = csv.DictWriter(out_f, fieldnames=columns_to_keep) writer.writeheader() # Write header to the output file for row in reader: # Keep only the columns of interest filtered_row = {col: row[col] for col in columns_to_keep} # Check if all values (except 'id') are zero, and skip such rows if all(int(filtered_row[col]) == 0 for col in columns_to_keep[1:]): continue # Skip rows where all selected columns (except 'id') are 0 # Write the filtered row to the output file writer.writerow(filtered_row) print(f"Filtered data saved to {output_csv}") # Main block to handle command line arguments if __name__ == "__main__": if len(sys.argv) != 4: print("Usage: python filter_csv.py ") sys.exit(1) input_csv = sys.argv[1] columns_file = sys.argv[2] output_csv = sys.argv[3] filter_csv(input_csv, columns_file, output_csv)