ChatGPT解决这个技术问题 Extra ChatGPT

如何将长字符串的定义拆分为多行?

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

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

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

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 库,如 SQLAlchemy 或其他东西,以避免像这样将原始 SQL 黑客攻击在一起。 (也许题外话,但你确实要求“任何建议”。;)
这是“为长字符串创建多行代码的 Pythonic 方式”要创建包含换行符的字符串,请参阅textwrap.dedent
@cezar 我在五年多前写了这个问题,但我记得它源于不知道如何正确地将长 sql 查询放在几行中。我同意我用那个长字符串做了一些愚蠢的事情,但这不是我的问题,而且我还不够聪明,无法寻找一个更好的例子来说明它不包括一些 sql 注入问题。
@cezar 不,这不是 XY 问题,无论如何,最好将查询格式化为多行。 SQLi 与手头的问题无关。然而,大胆的警告是完全合理的:)
我为此写了一个小包。此处示例:stackoverflow.com/a/56940938/1842491

L
Levon

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

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

您也可以使用单引号(其中 3 个当然在开头和结尾)并将生成的字符串 s 视为任何其他字符串。

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

IE,:

' 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 ...'

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


对于第二种方法,我更喜欢明确地使用“+”运算符。没有太多麻烦并提高了可读性。
@LucasMalor 相邻的字符串是编译时连接。使用 + 运算符不会使连接在运行时发生吗?
作为参考,这里是这个现象的官方文档:docs.python.org/2/reference/… (python 2) 和 docs.python.org/3/reference/… (python 3)
您的示例很好,但我希望它包括演示如何安全可靠地将变量数据嵌入到查询中。 OP 和@jessee 示例代码都显示了如何不正确地执行此操作(它们是 SQL 攻击的邀请)。另请参阅:dev.mysql.com/doc/connector-python/en/…
您可以使用 textwrap.dedent 删除不需要的前导空格。 docs.python.org/3/library/textwrap.html#textwrap.dedent
S
Stabledog

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

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 注入安全风险,因此请使用数据库的参数化查询功能来防止这种情况。但是,我将按原样保留答案,因为它直接回答了所提出的问题。


@Pablo 您甚至可以在 , 之后添加评论
@200OK 你的意思是在 ' 之后?
格式化此字符串的另一种方法是在右括号后添加 .format(...)% 格式化符号也必须有效,但我还没有尝试过
请注意,每一行必须以字符串常量结尾,因此 ' foo '+variable 不起作用,但 ' foo '+variable+'' 会。
此示例为 SQL 注入攻击打开了大门。请不要在任何面向公众的应用程序上使用它。有关如何使用“占位符”的信息,请参阅 MySQL 文档:dev.mysql.com/doc/connector-python/en/…
a
amphibient

\ 换行对我有用。这是一个例子:

longStr = "This is a very long string " \
        "that I wrote to help somebody " \
        "who had a question about " \
        "writing long strings in Python"

我更喜欢三引号表示法或在 () 内换行到 \ 字符
我强烈建议将空格放在下面几行的开头,而不是后面几行的结尾。这样一来,意外丢失的情况就更加明显(因此不太可能发生)。
也适用于行末的变量 longStr = "account: " + account_id + \ ...
我收到以下错误:the backslash is redundant between brackets 当我在里面写 print()
@Alfe 再也不用担心缺少 \'s 了。如果我错过一个 VScode 会心脏病发作
E
Eero Aaltonen

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

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

不同意。如果第一行(“string = ...”)严重缩进怎么办?必须将以下行缩进为零缩进,这在其他缩进块的中间看起来很难看。
好吧,我的大部分冗长字符串都出现在模块级别,这很适合。在你的情况下,这显然不是最好的解决方案。
我喜欢这种方法,因为它优先阅读。在我们有长字符串的情况下,没有办法...取决于您所处的缩进级别,并且仍然限制为每行 80 个字符...嗯...无需多说。在我看来,python 风格指南仍然很模糊。谢谢!
如果在模块下使用它会很丑陋,我也必须.replace('\t','')
如果您关心代码折叠,这将在大多数编辑器中破坏它。
P
Peter Mortensen

我发现在构建长字符串时,您通常会执行构建 SQL 查询之类的操作,在这种情况下这是最好的:

query = ' '.join((  # Note double parentheses. join() takes an iterable
    "SELECT foo",
    "FROM bar",
    "WHERE baz",
))

Levon suggested 是好的,但它可能容易出错:

query = (
    "SELECT foo"
    "FROM bar"
    "WHERE baz"
)

query == "SELECT fooFROM barWHERE baz"  # Probably not what you want

+1 使代码审阅者不必刻意检查每一行的右端是否存在不足的空白。正如@KarolyHorvath 所指出的,OP 多次犯了这个错误。
在查看以类似方式编码的多行字符串时,我需要在每行的左端有足够的空格以便于确认。
@BobStein-VisiBone 代码审查不应该是关于语法错误或像这样的小错误,它们应该是关于实质的。如果有人将有语法错误的代码提交审查(因此根本不会运行或在某些情况下不会运行),那么就会出现严重错误。在提交之前运行 lint 并不难。如果这个人因为犯了如此明显的错误而没有注意到他们的程序没有正确运行,那么他们就不应该犯下。
同意@CharlesAddis,代码审查应该在自动化方法之后进行,例如 lint、语法突出显示等。但是,可能不会以这种方式捕获一些缺少空白的错误。我建议,利用所有合理的优势来防范错误。
额外的好处是您可以快速排除注释行。
P
Peter Mortensen

这种方法使用:

使用三引号字符串几乎没有内部标点符号

使用检查模块去除局部缩进

对 account_id 和 def_id 变量使用 Python 3.6 格式化字符串插值 ('f')。

这种方式在我看来是最 Pythonic 的。

import inspect

query = inspect.cleandoc(f'''
    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}'''
)

注意:inspect.cleandoc is slightly nicertextwrap.dedent,因为它不需要第一行为空且末尾有行继续符。
@ShadowRanger 哇,我以前从未使用过 cleandoc。我更新了我的答案,将来会为此使用 inspect.cleandoc
这对我很有用!去掉在 ''' 报价过程中从编辑器中添加的空格!
虽然看起来很不错,但我认为这种方法很容易受到 SQL 注入的攻击。遗憾的是,f-string 不适用于 SQL 查询。从其他评论来看,最好使用 cursor.execute 而不是 dev.mysql.com/doc/connector-python/en/…
P
Peter Mortensen

您还可以在使用 """ 表示法时包含变量:

foo = '1234'

long_string = """fosdl a sdlfklaskdf as
as df ajsdfj asdfa sld
a sdf alsdfl alsdfl """ +  foo + """ aks
asdkfkasdk fak"""

更好的方法是使用命名参数和 .format():

body = """
<html>
<head>
</head>
<body>
    <p>Lorem ipsum.</p>
    <dl>
        <dt>Asdf:</dt>     <dd><a href="{link}">{name}</a></dd>
    </dl>
    </body>
</html>
""".format(
    link='http://www.asdf.com',
    name='Asdf',
)

print(body)

在这里使用 f strings 似乎更加自然和容易。
V
Vlad Bezden

在 Python >= 3.6 中,您可以使用 Formatted string literals (f string)

query= f'''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}'''

如果我想记录多行字符串的结果并且不显示左侧制表符/空格,那么 f 字符串将如何工作?
仍然容易受到 SQL 注入的影响
f
fredrik

我发现 textwrap.dedent 最适合描述 here 的长字符串:

def create_snippet():
    code_snippet = textwrap.dedent("""\
        int main(int argc, char* argv[]) {
            return 0;
        }
    """)
    do_something(code_snippet)

我喜欢防止自动换行的黑色斜线,非常感谢!
如果您使用 inspect.cleandoc 而不是 textwrap.dedent,则不需要反斜杠。
这在犯罪上被低估了。
IMO 这是这里最好的解决方案之一,感谢您揭示 Python 标准库的另一个不错的部分
P
Peter Mortensen

例如:

sql = ("select field1, field2, field3, field4 "
       "from table "
       "where condition1={} "
       "and condition2={}").format(1, 2)

Output: 'select field1, field2, field3, field4 from table
         where condition1=1 and condition2=2'

如果条件的值应该是一个字符串,你可以这样做:

sql = ("select field1, field2, field3, field4 "
       "from table "
       "where condition1='{0}' "
       "and condition2='{1}'").format('2016-10-12', '2017-10-12')

Output: "select field1, field2, field3, field4 from table where
         condition1='2016-10-12' and condition2='2017-10-12'"

d
ddrscott

其他人已经提到了括号方法,但我想用括号添加它,允许内联注释。

评论每个片段:

nursery_rhyme = (
    'Mary had a little lamb,'          # Comments are great!
    'its fleece was white as snow.'
    'And everywhere that Mary went,'
    'her sheep would surely go.'       # What a pesky sheep.
)

继续后不允许评论:

使用反斜杠续行符 (\ ) 时,不允许使用注释。您将收到 SyntaxError: unexpected character after line continuation character 错误。

nursery_rhyme = 'Mary had a little lamb,' \  # These comments
    'its fleece was white as snow.'       \  # are invalid!
    'And everywhere that Mary went,'      \
    'her sheep would surely go.'
# => SyntaxError: unexpected character after line continuation character

对正则表达式字符串的更好评论:

根据 https://docs.python.org/3/library/re.html#re.VERBOSE 中的示例,

a = re.compile(
    r'\d+'  # the integral part
    r'\.'   # the decimal point
    r'\d*'  # some fractional digits
)
# Using VERBOSE flag, IDE usually can't syntax highight the string comment.
a = re.compile(r"""\d +  # the integral part
                   \.    # the decimal point
                   \d *  # some fractional digits""", re.X)

P
Peter Mortensen

作为在 Python 中处理长字符串的一般方法,您可以使用三引号 splitjoin

_str = ' '.join('''Lorem ipsum dolor sit amet, consectetur adipiscing
        elit, sed do eiusmod tempor incididunt ut labore et dolore
        magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
        ullamco laboris nisi ut aliquip ex ea commodo.'''.split())

输出:

'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo.'

关于 OP 关于 SQL 查询的问题,下面的答案忽略了这种构建 SQL 查询的方法的正确性,只关注以可读和美观的方式构建长字符串,而不需要额外的导入。它也忽略了这带来的计算负载。

使用三引号,我们构建了一个长且可读的字符串,然后我们使用 split() 将其分解为一个列表,从而去除空白,然后将其与 ' '.join() 重新连接在一起。最后,我们使用 format() 命令插入变量:

account_id = 123
def_id = 321

_str = '''
    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 = {}
    AND record_def.account_id = {}
    AND def_id = {}
    '''

query = ' '.join(_str.split()).format(account_id, account_id, def_id)

产生:

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 = 123 AND record_def.account_id=123 AND def_id=321

这种方法不符合 PEP 8,但我发现它有时很有用。

请注意,原始字符串中的大括号由 format() 函数使用。


F
Faheel

我个人认为以下是在 Python 中编写原始 SQL 查询的最佳(简单、安全和 Pythonic)方法,尤其是在使用 Python's sqlite3 module 时:

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 = ?
        AND record_def.account_id = ?
        AND def_id = ?
'''
vars = (account_id, account_id, def_id)   # a tuple of query variables
cursor.execute(query, vars)   # using Python's sqlite3 module

优点

简洁的代码(Pythonic!)

远离 SQL 注入

与 Python 2 和 Python 3 兼容(毕竟它是 Pythonic)

不需要字符串连接

无需确保每行最右边的字符是空格

缺点

由于查询中的变量被替换为 ?占位符,跟踪哪个可能会变得有点困难?当查询中有很多变量时,将被哪个 Python 变量替换。


请注意,我没有对此进行测试,但是您可以通过在相关位置将它们替换为“{0} cursor.execute(query.format(vars)) {2}”然后将最后一行更改为 cursor.execute(query.format(vars)) 来避免问号混淆。那应该照顾你唯一的“骗局”(我希望)。
是的,使用 format 会很好,但我不确定以这种方式格式化的查询字符串是否可以防止 SQL 注入。
是的,这是一个公平的观点,它肯定会变得有点棘手。也许在完全可消耗的东西上测试它是明智的......毫无疑问是一个比较。科学。本科生很快就会过去。 ;)
@Ben 如果您这样做 cursor.execute(query.format(vars)) 您将不再从准备好的语句中受益,因此您很容易受到多种问题的影响,首先是如果参数不仅仅是数字,您需要在 SQL 查询中将它们双引号。
P
Peter Mortensen

tl;dr:使用 """\""" 包装字符串,如

string = """\
This is a long string
spanning multiple lines.
"""

official Python documentation

字符串文字可以跨越多行。一种方法是使用三引号:"""...""" 或 '''...'''。行尾自动包含在字符串中,但可以通过在行尾添加 \ 来防止这种情况。下面的例子:

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

产生以下输出(请注意,不包括初始换行符):

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

我从来不知道你可以用多行字符串文字做到这一点,谢谢:)
u
user41855

添加到@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 ...'

U
U12-Forward

我通常使用这样的东西:

text = '''
    This string was typed to be a demo
    on how could we write a multi-line
    text in Python.
'''

如果要删除每行中烦人的空格,可以执行以下操作:

text = '\n'.join(line.lstrip() for line in text.splitlines())

查看 Python 的 textwrap.dedent 函数,它位于标准库中,它具有您需要的功能。
@bjd2385:inspect.cleandoc 稍微好一点(关于文本是否与开引号出现在同一行,不需要明确的行继续字符)。
R
Rick

嗯。

我知道这个问题发布已经很久了。但我刚刚找到了我想用来为我的项目中的变量分配长字符串和多行字符串的样式。这需要一些额外的运行时间,但仍然保留了代码的美感,即使我分配字符串的变量是严重缩进的。

    # Suppose the following code is heavily indented
    line3header = "Third"

    variable = fr"""

First line.
Second line.
{line3header} line.
{{}} line.
...
The last line.

    """.strip()
    """A variable whose name is Variable.

    You can even add a docstring here.
    """

    variable = variable.format("Fourth")
    print(variable)
    variable += "\n"
    print(variable, end="")

就这样。


P
Peter Mortensen

您的实际代码不应该工作;您在“行”末尾缺少空格(例如,role.descr as roleFROM...)。

多行字符串有三引号:

string = """line
  line2
  line3"""

它将包含换行符和额外的空格,但对于 SQL 来说这不是问题。


P
Peter Mortensen

尝试这样的事情。就像这种格式一样,它会返回一条连续的线,就像您已成功查询此属性一样:

"message": f'You have successfully inquired about '
           f'{enquiring_property.title} Property owned by '
           f'{enquiring_property.client}'

P
Peter Mortensen

结合以下想法:

LevonJesseFaheelddrscott

使用我的格式建议,您可以将查询编写为:

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(options))' 一起使用可能会很有趣,其中:

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

或者根据 darkfeline 的提示,您可能仍然会在使用前导空格和分隔符以及命名占位符时出错:

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

请参阅documentation of Cursor.execute-function

“这是 [最 Pythonic] 的方式!” - ...


Б
Богдан Божић

我知道这是一个相当老的问题,但与此同时 Python 发生了变化,我没有看到这个答案,所以我们开始吧。

另一种方法是使用 \ 剪切当前行并移动到另一行:

print("This line will \
get carried over to\
 the new line.\
Notice how this\
word will be together because \
of no space around it")

请注意,这不适用于缩进代码(在美学上)。
P
Peter Mortensen

您还可以将 SQL 语句放在单独的文件 action.sql 中,然后将其加载到 .py 文件中:

with open('action.sql') as f:
   query = f.read()

因此 SQL 语句将与 Python 代码分离。如果 SQL 语句中有参数需要从 Python 中填充,您可以使用字符串格式(如 %s 或 {field})。


P
Peter Mortensen

当代码(例如,变量)缩进并且输出字符串应该是单行(没有换行符)时,我认为另一个选项更具可读性:

def some_method():

    long_string = """
A presumptuous long string
which looks a bit nicer
in a text editor when
written over multiple lines
""".strip('\n').replace('\n', ' ')

    return long_string

P
Peter Mortensen

“À la” Scala 方式(但我认为这是 OP 要求的最 Pythonic 方式):

description = """
            | The intention of this module is to provide a method to
            | pass meta information in markdown_ header files for
            | using it in jinja_ templates.
            |
            | Also, to provide a method to use markdown files as jinja
            | templates. Maybe you prefer to see the code than
            | to install it.""".replace('\n            | \n','\n').replace('            | ',' ')

如果您希望最终 str 没有跳转行,只需将 \n 放在第二个替换的第一个参数的开头:

.replace('\n            | ',' ')`.

注意:“...模板”之间的白线。并且“另外,...”在 | 之后需要一个空格。


P
Peter Mortensen

official Python documentation

字符串文字可以跨越多行。一种方法是使用三引号:"""...""" 或 '''...'''。行尾自动包含在字符串中,但可以通过在行尾添加 \ 来防止这种情况。下面的例子:

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

产生以下输出(请注意,不包括初始换行符):


P
Peter Mortensen

我使用递归函数来构建复杂的 SQL 查询。这种技术通常可用于构建大字符串,同时保持代码的可读性。

# 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)

PS:如果需要,请查看很棒的 python-sqlparse 库以漂亮地打印 SQL 查询。


“递归函数”不就是叫lambda吗?
f
flix

为了在字典中定义一个长字符串,保留换行符但省略空格,我最终将字符串定义为一个常量,如下所示:

LONG_STRING = \
"""
This is a long sting
that contains newlines.
The newlines are important.
"""

my_dict = {
   'foo': 'bar',
   'string': LONG_STRING
}

P
Peter Mortensen

我喜欢这种方法,因为它优先阅读。在我们有长字符串的情况下,没有办法!根据您所处的缩进级别,每行仍限制为 80 个字符...嗯...无需多说

在我看来,Python 风格指南仍然很模糊。我选择了 Eero Aaltonen approach,因为它具有阅读和常识的特权。我知道风格指南应该帮助我们,而不是让我们的生活一团糟。

class ClassName():
    def method_name():
        if condition_0:
            if condition_1:
                if condition_2:
                    some_variable_0 =\
"""
some_js_func_call(
    undefined,
    {
        'some_attr_0': 'value_0',
        'some_attr_1': 'value_1',
        'some_attr_2': '""" + some_variable_1 + """'
    },
    undefined,
    undefined,
    true
)
"""

P
Peter Mortensen

通常,我将 listjoin 用于多行注释/字符串。

lines = list()
lines.append('SELECT action.enter code here descr as "action", ')
lines.append('role.id as role_id,')
lines.append('role.descr as role')
lines.append('FROM ')
lines.append('public.role_action_def,')
lines.append('public.role,')
lines.append('public.record_def, ')
lines.append('public.action')
query = " ".join(lines)

您可以使用任何字符串来连接所有这些列表元素,例如“\n”(换行符)或“,”(逗号)或“ ”(空格)。


你为什么不至少使用一个数组文字?
我想这可行,但你应该考虑性能和可读性......