-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaning.py
50 lines (30 loc) · 1019 Bytes
/
cleaning.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
#!/usr/bin/local/python
import csv
import os
import sys
from dbfpy import dbf
# Globals/Relative references
DIR = os.getcwd()
INPUT = DIR + "example-data/Price data from year 2003 to date/"
BASE_YEAR = 2003
def DBFtoCSV(filename):
if filename.endswith('.DBF'):
print "Converting %s to csv" % filename
csv_fn = filename[:-4]+ ".csv"
with open(csv_fn,'wb') as csvfilename:
in_db = dbf.Dbf(filename)
out_csv = csv.writer(csvfilename)
names = []
for field in in_db.header.fields:
names.append(field.name)
out_csv.writerow(names)
for rec in in_db:
out_csv.writerow(rec.fieldData)
in_db.close()
print "Done..."
else:
print "Filename does not end with .DBF"
for folder in os.listdir(INPUT):
if folder!=".DS_Store" and folder[-4:]!="xlsx":
for item in os.listdir(INPUT + folder):
DBFtoCSV(INPUT + folder + "/" + item)