Skip to content

Instantly share code, notes, and snippets.

@susodapop
Created August 16, 2019 12:57
Show Gist options
  • Save susodapop/91548c797376521d9bda07e8837c5d74 to your computer and use it in GitHub Desktop.
Save susodapop/91548c797376521d9bda07e8837c5d74 to your computer and use it in GitHub Desktop.
Use this snippet to convert an input CSV file into a query for Redash. This is nifty if you can't host your CSV. This only works with up to 500 rows of data, so it's a bit of an edge case.
import csv
import re
import string
with open('MOCK_DATA.csv', newline='\n', encoding="cp437") as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
headers = spamreader.__next__();
pat = re.compile = r"'"
repl = r"''"
statements = []
counter = 0
for row in spamreader:
if counter > 495:
break
this_sql = "SELECT " + ','.join(
[f"'{re.sub(pat, repl, val)}' as \"{key}\"" for key, val in zip(headers, row)]) + '\n'
statements.append(this_sql)
counter += 1
with open('output.sql', 'w') as fp:
fp.write('UNION ALL\n'.join(statements))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment