Pythonic way to create a long multi-line string

后端 未结 27 1652
滥情空心
滥情空心 2020-11-22 00:47

I have a very long query. I would like to split it in several lines in Python. A way to do it in JavaScript would be using several sentences and joining them with a +<

27条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 00:56

    I use a recursive function to build complex SQL Queries. This technique can generally be used to build large strings while maintaining code readability.

    # Utility function to recursively resolve SQL statements.
    # CAUTION: Use this function carefully, Pass correct SQL parameters {},
    # TODO: This should never happen but check for infinite loops
    def resolveSQL(sql_seed, sqlparams):
        sql = sql_seed % (sqlparams)
        if sql == sql_seed:
            return ' '.join([x.strip() for x in sql.split()])
        else:
            return resolveSQL(sql, sqlparams)
    

    P.S: Have a look at the awesome python-sqlparse library to pretty print SQL queries if needed. http://sqlparse.readthedocs.org/en/latest/api/#sqlparse.format

提交回复
热议问题