Created
August 16, 2019 12:57
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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