-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbf2csv.py
More file actions
33 lines (27 loc) · 778 Bytes
/
Copy pathdbf2csv.py
File metadata and controls
33 lines (27 loc) · 778 Bytes
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
#!/usr/bin/python
import csv
from dbfpy import dbf
from os import rename
import sys
from glob import glob
filename = sys.argv[1]
listdb = glob('%s.*' % filename)
for dbFile in listdb:
rename(dbFile, dbFile.lower())
filename = '%s.dbf' % filename.lower()
if filename.endswith('.dbf'):
print "Converting %s to csv" % filename
csv_fn = filename[:-4]+ ".csv"
with open(csv_fn,'wb') as csvfile:
in_db = dbf.Dbf(filename)
out_csv = csv.writer(csvfile)
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"