-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathscourgify.py
82 lines (55 loc) · 1.77 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
############################################
# #
# by : Ali Sharify #
# Dont copy currently Code #
# Github: alisharifyy #
# #
############################################
import os,sys, csv
# header of new csv file
file_headers = ["first", "last", "house"]
def check_command_line():
"""
This function check the command line argument is correct by the rule
"""
if len(sys.argv) > 3:
sys.exit("to many")
if len(sys.argv) < 3:
sys.exit("to Few")
def nomalize_data(oldfile):
"""
This function take an csv and return all data in correct format in list
"""
db = []
reader = csv.DictReader(oldfile)
for each in reader:
temp = {}
temp["last"], temp["first"] = each["name"].split(",")
# remove white spaces
temp["first"] = temp["first"].strip()
temp["last"] = temp["last"].strip()
temp["house"] = each["house"]
db.append(temp)
return db
def cr_csv(data, filename):
"""
This function take an data in form of list and create new csv
and write to ot
"""
with open(filename, "w", encoding="UTF8", newline="") as file:
writer = csv.DictWriter(file, fieldnames=file_headers)
writer.writeheader()
writer.writerows(data)
def main():
check_command_line()
try:
file = open(sys.argv[1], "r", encoding="utf-8")
except FileNotFoundError:
sys.exit(f"{sys.argv[1]} not found")
# clean data
data = nomalize_data(file)
# create new file
cr_csv(data, sys.argv[2])
file.close()
if __name__ == "__main__":
main()