Skip to content

Instantly share code, notes, and snippets.

@bjorndown
Last active December 26, 2015 18:29
Show Gist options
  • Save bjorndown/7195299 to your computer and use it in GitHub Desktop.
Save bjorndown/7195299 to your computer and use it in GitHub Desktop.
Tries to transform a bunch of INSERT statements into a CSV file.
import re
import sys
PATTERN = re.compile("INSERT INTO ([\w_\.]+) \(([\w,\"]+)\) VALUES \(([\w ,:'\.\(\)]+)\);$", flags=re.IGNORECASE)
TO_DATE_PATTERN = "to_date\(('[0-9 :\.]+'),'DD.MM.RR HH24:MI:SS'\)"
def convert(filename):
with open(filename) as source_file:
output = None
current_table_name = None
for line in source_file:
match = PATTERN.search(line)
if match:
table = match.group(1)
header = match.group(2)
row = match.group(3)
if output is None:
output = create_new_csv(table, header, filename)
if not current_table_name == table:
output.close()
output = create_new_csv(table, header, filename)
current_table_name = table
processed_row = process_row(row)
print(processed_row, file=output)
output.close()
def create_new_csv(table, header, filename):
output = open('{}-{}.csv'.format(filename, table), 'w')
print(header, file=output)
return output
def process_row(row):
return re.sub(TO_DATE_PATTERN, '\g<1>', row)
if __name__ == '__main__':
convert(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment