-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScourgify
More file actions
39 lines (32 loc) · 1.06 KB
/
Scourgify
File metadata and controls
39 lines (32 loc) · 1.06 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
import sys
import csv
def main():
# Check command-line arguments
if len(sys.argv) < 3:
sys.exit("Too few command-line arguments")
elif len(sys.argv) > 3:
sys.exit("Too many command-line arguments")
input_file = sys.argv[1]
output_file = sys.argv[2]
# Try to read the input file
try:
with open(input_file, "r") as infile:
reader = csv.DictReader(infile)
students = []
for row in reader:
last, first = row["name"].split(", ")
students.append({
"first": first,
"last": last,
"house": row["house"]
})
except FileNotFoundError:
sys.exit(f"Could not read {input_file}")
# Write to the output file
with open(output_file, "w", newline="") as outfile:
writer = csv.DictWriter(outfile, fieldnames=["first", "last", "house"])
writer.writeheader()
for student in students:
writer.writerow(student)
if __name__ == "__main__":
main()