-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_csv_for_average_points.py
More file actions
executable file
·53 lines (35 loc) · 1.73 KB
/
extract_csv_for_average_points.py
File metadata and controls
executable file
·53 lines (35 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python3
# This output certain file from the allrider.csv file for Run and Ride.
import csv
import argparse
# Specify the desired columns and their order
DESIRED_COLUMNS = ["Entry type", "Last name", "First name", "Membership number"]
def filter_csv(input_file, output_file):
"""Filter and write specific columns from the input CSV to the output CSV."""
with open(input_file, mode="r", newline="", encoding="utf-8") as infile:
reader = csv.DictReader(infile)
# Verify if all desired columns exist in the input file
missing_columns = [col for col in DESIRED_COLUMNS if col not in reader.fieldnames]
if missing_columns:
raise ValueError(f"Missing columns in input file: {', '.join(missing_columns)}")
# Open the output file for writing
with open(output_file, mode="w", newline="", encoding="utf-8") as outfile:
writer = csv.DictWriter(outfile, fieldnames=DESIRED_COLUMNS)
# Write the header row
writer.writeheader()
# Write filtered rows
for row in reader:
filtered_row = {col: row[col] for col in DESIRED_COLUMNS}
writer.writerow(filtered_row)
print(f"Filtered data written to {output_file}")
def main():
# Set up argument parser
parser = argparse.ArgumentParser(description="Filter specific columns from a CSV file.")
parser.add_argument("input_file", type=str, help="Path to the input CSV file.")
parser.add_argument("output_file", type=str, help="Path to the output CSV file.")
# Parse the arguments
args = parser.parse_args()
# Call the function to filter the CSV
filter_csv(args.input_file, args.output_file)
if __name__ == "__main__":
main()