-
Notifications
You must be signed in to change notification settings - Fork 3
/
scourgify.py
28 lines (26 loc) · 1.01 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
from sys import exit, argv
import csv
if len(argv) < 3:
exit("Too few command-line arguments")
elif len(argv) > 3:
exit("Too many command-line arguments")
else:
if argv[1][-4:] == ".csv" and argv[2][-4:] == ".csv":
try:
f = open(argv[1])
except FileNotFoundError:
exit(f"Could not read {argv[1]}")
else:
listofdict = []
with f as file:
reader = csv.DictReader(file)
for row in reader:
x, y = row["name"].split(', ')
listofdict.append({"first": y, "last": x, "house": row["house"]})
with open(argv[2], "w") as file:
writer = csv.DictWriter(file, fieldnames=["first", "last", "house"])
writer.writeheader()
for i in range(len(listofdict)):
writer.writerow({"first": listofdict[i]["first"], "last": listofdict[i]["last"], "house": listofdict[i]["house"]})
else:
exit("Not a CSV file")