insert_every = 1000
for line in file_to_parse:
    m = line_regex.match(line)
if m:
    fields = m.groupdict()
if use_sql:
    inserts.append(pythonified)
if parsed % insert_every == 0:
    connection.execute(log_table.insert(), inserts)
inserts = []
parsed += 1
if use_sql:
    if len(inserts) > 0:
        connection.execute(log_table.insert(), inserts)
Faster Insertion of Records into a Table with SQLAlchemy