1
+ # -*- coding: utf-8 -*-
2
+ # @Author: Miclain Keffeler
3
+ # @Date: 2017-02-25 10:15:52
4
+ # @Last Modified by: Miclain Keffeler
5
+ # @Last Modified time: 2017-02-25 15:03:34
1
6
import csv
2
7
import os
3
8
import sys
4
9
5
10
linecount = 0
6
- with open ('Data/Health_InspViolations.csv' , 'r' ) as csvfile :
11
+ input_file = 'raw_data/Health_InspViolations.csv'
12
+ output_file = 'clean_data/Health_InspViolations.csv'
13
+
14
+ #Open the input and output files
15
+ with open (input_file , 'r' ) as csvfile :
7
16
rdr = csv .reader (csvfile )
8
- with open ("CleanData/Health_InspViolations.csv" ,"w" ) as csvfile1 :
17
+ with open (output_file ,"w" ) as csvfile1 :
9
18
wtr = csv .writer (csvfile1 )
19
+ #Add the appropriate column headers to the file
10
20
entry = 'inspection_id,weight,critical_yn'
11
21
new_header = entry [0 :32 ]
12
22
wtr .writerow ([new_header ])
23
+
13
24
for line in rdr :
25
+ #If this is not the first line in the file
14
26
if (linecount != 0 ):
15
27
linecount = linecount + 1
28
+ #Strip the double quotes and output
16
29
clean_line = [entry .strip ('"' ) for entry in line ]
17
30
wtr .writerow (clean_line )
18
31
for line in rdr :
32
+ #Add inspection id, weight to new row
19
33
new_row = line [1 :2 ]+ line [4 :5 ]
20
- #wtr.writerow(new_row)
34
+
35
+ #If the critical y/n value is No, add a 0 to new_row and write it
21
36
if (line [5 :6 ]== ['No' ]):
22
37
liste = []
23
38
liste = [0 ]
24
39
new_row = line [1 :2 ]+ line [4 :5 ]+ liste [0 :1 ]
25
40
wtr .writerow (new_row )
41
+ #Else, add a 1 to new_row and write it
26
42
else :
27
43
lister = []
28
44
lister = [1 ]
29
45
new_row = line [1 :2 ]+ line [4 :5 ]+ lister [0 :1 ]
30
- wtr .writerow (new_row )
31
-
32
-
33
- #new_row = line[0:1] + line[4:5] + line[5:6]
34
-
46
+ wtr .writerow (new_row )
35
47
0 commit comments