-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscourgify.py
36 lines (28 loc) · 1.15 KB
/
scourgify.py
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
import csv
import sys
import argparse
def process_csv(input_file, output_file):
try:
with open(input_file) as input, open(output_file, "w", newline="") as output:
reader = csv.DictReader(input)
writer = csv.DictWriter(output, fieldnames=["first", "last", "house"])
writer.writeheader()
for row in reader:
last_name, first_name = row["name"].strip().split(", ")
writer.writerow(
{"first": first_name, "last": last_name, "house": row["house"]}
)
except FileNotFoundError:
sys.exit(f"Error: File '{input_file}' does not exist.")
except ValueError as ve:
sys.exit(f"Error: {ve}")
def main():
parser = argparse.ArgumentParser(description="Process CSV file.")
parser.add_argument("input_file", help="Input CSV file")
parser.add_argument("output_file", help="Output CSV file")
args = parser.parse_args()
if not args.input_file.endswith(".csv"):
sys.exit("Error: Input file must have a .csv extension.")
process_csv(args.input_file, args.output_file)
if __name__ == "__main__":
main()