-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql2csv.py
54 lines (43 loc) · 1.29 KB
/
sql2csv.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
import re
import sys
def get_file_paths():
#remove THIS filename from sys.argv if it's in there
args = sys.argv
if __file__.rstrip('.py') in args[0]:
del args[0]
try:
input_file_path = args[0]
except IndexError:
print "Usage: sql2csv input_file [output_file]"
sys.exit(1)
try:
output_file_path = args[1]
except IndexError:
output_file_path = "%s-sql2csv.csv" % input_file_path
return input_file_path, output_file_path
def is_separator(line):
""" Is the line just +-----+----+ ?"""
return re.match("\+[+-]+\+", line)
def convert_line(line):
#TODO: this could be better, as at the moment it removes all whitespace
line = line.strip("|")
line = line.replace("|", ",")
line = line.replace(" ", "")
return line
def convert_file(sql_file, csv_file):
for line in sql_file.readlines():
if is_separator(line):
continue
line = convert_line(line)
csv_file.write(line)
def main():
input_file_path, output_file_path = get_file_paths()
sql_file = open(input_file_path, "r")
csv_file = open(output_file_path, "w")
convert_file(sql_file, csv_file)
sql_file.close()
csv_file.close()
print "Done"
sys.exit(0)
if __name__ == "__main__":
main()