I have problem with cleaning comments and empty lines from already existing sql file. The file has over 10k lines so cleaning it manually is not an option.
I have a litt
This is an extend of samplebias answer that work with your example :
import sqlparse
sql_example = """--comment
SELECT * from test;
INSERT INTO test VALUES ('
-- test
a
');
"""
new_sql = []
for statement in sqlparse.parse(sql_example):
new_tockens = [stm for stm in statement.tokens
if not isinstance(stm, sqlparse.sql.Comment)]
new_statement = sqlparse.sql.TokenList(new_tockens)
new_sql.append(new_statement.to_unicode())
print sqlparse.format("\n".join(new_sql))
Output:
SELECT * from test;
INSERT INTO test VALUES ('
-- test
a
');