Skip to content

Instantly share code, notes, and snippets.

@jsburklund
Created July 31, 2018 04:59
Show Gist options
  • Save jsburklund/9750239b8c5336f73b57fea2e0876fbc to your computer and use it in GitHub Desktop.
Save jsburklund/9750239b8c5336f73b57fea2e0876fbc to your computer and use it in GitHub Desktop.
Parse SQL files for variables and replace them with python
def parser(filename, values):
with open(filename, 'r') as f:
sql_text = f.read()
sql_text = sql_text.replace("$[", "{")
sql_text = sql_text.replace("]", "}")
sql_text = sql_text.format(**values)
print(sql_text)
if __name__=='__main__':
parser("test.sql", {"year": 2018})
parser("test.sql", {"year": 1903})
for year in [2016, 2017, 2019]:
parser("test.sql", {"year": year})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment