我有一个很长的疑问。我想用Python将它分成几行。在JavaScript中实现这一点的一种方法是使用几个句子并用+运算符将它们连接起来(我知道,也许这不是最有效的方法,但我并不真正关心这个阶段的性能,只是代码的可读性)。例子:

var long_string = 'some text not important. just garbage to' +
                      'illustrate my example';

我尝试在Python中做类似的事情,但没有成功,所以我使用了\来拆分长字符串。然而,我不确定这是否是唯一/最好/最蟒蛇的做法。看起来很尴尬。实际代码:

query = 'SELECT action.descr as "action", '\
    'role.id as role_id,'\
    'role.descr as role'\
    'FROM '\
    'public.role_action_def,'\
    'public.role,'\
    'public.record_def, '\
    'public.action'\
    'WHERE role.id = role_action_def.role_id AND'\
    'record_def.id = role_action_def.def_id AND'\
    'action.id = role_action_def.action_id AND'\
    'role_action_def.account_id = ' + account_id + ' AND'\
    'record_def.account_id=' + account_id + ' AND'\
    'def_id=' + def_id

当前回答

如果不需要多行字符串,而只需要一个长的单行字符串,则可以使用括号。只需确保字符串段之间不包含逗号(那么它将是一个元组)。

query = ('SELECT   action.descr as "action", '
         'role.id as role_id,'
         'role.descr as role'
         ' FROM '
         'public.role_action_def,'
         'public.role,'
         'public.record_def, '
         'public.action'
         ' WHERE role.id = role_action_def.role_id AND'
         ' record_def.id = role_action_def.def_id AND'
         ' action.id = role_action_def.action_id AND'
         ' role_action_def.account_id = '+account_id+' AND'
         ' record_def.account_id='+account_id+' AND'
         ' def_id='+def_id)

在您正在构建的SQL语句中,多行字符串也可以。但是,如果多行字符串包含的额外空白是一个问题,那么这将是一个实现您所需功能的好方法。

如注释中所述,以这种方式连接SQL查询是SQL注入安全风险,因此请使用数据库的参数化查询功能来防止这种情况发生。然而,我保留了答案,因为它直接回答了问题。

其他回答

我发现自己对这个很满意:

string = """This is a
very long string,
containing commas,
that I split up
for readability""".replace('\n',' ')

PEP 8风格指南建议使用括号:

包装长行的首选方式是在括号、括号和大括号内使用Python的隐含行延续。通过将表达式括在括号中,可以在多行上打断长行。应优先使用这些字符,而不是使用反斜杠作为换行符。

例子:

long_string = (
    "This is a lengthy string that takes up a lot of space. I am going to "
    "keep on typing words to fill up more and more space to show how you can "
    "split the string across multiple lines."
)

添加@Levon的答案。。。。

1.创建如下多行字符串:

paragraph = """this is a very
        long string if I had the
        energy to type more and more ..."""

print(paragraph)

输出:

'this is a very\n        long string if I had the\n        energy to type more and more ...'

此字符串将包含换行符和空格。因此,请移除它们。

2.使用正则表达式删除多余的空格

paragraph = re.sub('\s+', ' ', paragraph)
print(paragraph)

输出:

'this is a very long string if I had the energy to type more and more ...'

你说的是多行字符串吗?简单,使用三引号开始和结束它们。

s = """ this is a very
        long string if I had the
        energy to type more and more ..."""

您也可以使用单引号(当然,在开始和结束处有3个引号),并像对待任何其他字符串一样对待生成的字符串。

注意:与任何字符串一样,起始引号和结束引号之间的任何内容都将成为字符串的一部分,因此本示例有一个前导空格(如@root45所指出的)。此字符串还将包含空格和换行符。

即。,:

' this is a very\n        long string if I had the\n        energy to type more and more ...'

最后,您还可以在Python中构造如下的长行:

 s = ("this is a very"
      "long string too"
      "for sure ..."
     )

这将不包括任何额外的空格或换行符(这是一个故意的示例,显示跳过空格的效果):

'this is a verylong string toofor sure ...'

不需要逗号,只需将要连接在一起的字符串放入一对括号中,并确保考虑到任何需要的空格和换行符。

结合以下观点:

Levon或Jesse、Faheel和ddrscott

根据我的格式建议,您可以将查询写成:

query = ('SELECT'
             ' action.descr as "action"'
             ',role.id as role_id'
             ',role.descr as role'
         ' FROM'
             ' public.role_action_def'
             ',public.role'
             ',public.record_def'
             ',public.action'
         ' WHERE'
             ' role.id = role_action_def.role_id'
             ' AND'
             ' record_def.id = role_action_def.def_id'
             ' AND'
             ' action.id = role_action_def.action_id'
             ' AND'
             ' role_action_def.account_id = ?' # account_id
             ' AND'
             ' record_def.account_id = ?'      # account_id
             ' AND'
             ' def_id = ?'                     # def_id
         )

 vars = (account_id, account_id, def_id)     # A tuple of the query variables
 cursor.execute(query, vars)                 # Using Python's sqlite3 module

或类似:

vars = []
query = ('SELECT'
             ' action.descr as "action"'
             ',role.id as role_id'
             ',role.descr as role'
         ' FROM'
             ' public.role_action_def'
             ',public.role'
             ',public.record_def'
             ',public.action'
         ' WHERE'
             ' role.id = role_action_def.role_id'
             ' AND'
             ' record_def.id = role_action_def.def_id'
             ' AND'
             ' action.id = role_action_def.action_id'
             ' AND'
             ' role_action_def.account_id = '
                 vars.append(account_id) or '?'
             ' AND'
             ' record_def.account_id = '
                 vars.append(account_id) or '?'
             ' AND'
             ' def_id = '
                 vars.append(def_id) or '?'
         )

 cursor.execute(query, tuple(vars))  # Using Python's sqlite3 module

与“IN”和“vars.extend(options)”或“n_options(len(option))”一起使用可能很有趣,其中:

def n_options(count):
    return '(' + ','.join(count*'?') + ')'

或者从darkcaline那里得到的提示是,您可能仍然会在使用前导空格和分隔符以及命名占位符时出错:

SPACE_SEP = ' '
COMMA_SEP = ', '
AND_SEP   = ' AND '

query = SPACE_SEP.join((
    'SELECT',
        COMMA_SEP.join((
        'action.descr as "action"',
        'role.id as role_id',
        'role.descr as role',
        )),
    'FROM',
        COMMA_SEP.join((
        'public.role_action_def',
        'public.role',
        'public.record_def',
        'public.action',
        )),
    'WHERE',
        AND_SEP.join((
        'role.id = role_action_def.role_id',
        'record_def.id = role_action_def.def_id',
        'action.id = role_action_def.action_id',
        'role_action_def.account_id = :account_id',
        'record_def.account_id = :account_id',
        'def_id = :def_id',
        )),
    ))

vars = {'account_id':account_id,'def_id':def_id}  # A dictionary of the query variables
cursor.execute(query, vars)                       # Using Python's sqlite3 module

请参阅Cursor.execute-function的文档。

“这是[最Python]的方式!”-。。。