【问题标题】:Pythonic way to create a long multi-line string创建长多行字符串的 Pythonic 方法
【发布时间】:2020-11-27 02:20:07
【问题描述】:

我有一个很长的问题。我想在 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

标签: python string multiline multilinestring


【解决方案1】:

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

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

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

注意:就像任何字符串一样,开头和结尾引号之间的任何内容都将成为字符串的一部分,因此此示例具有前导空格(如@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 ...'

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

【讨论】:

【解决方案2】:

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

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 语句中,多行字符串也可以。但是,如果多行字符串包含的额外空格会成为问题,那么这将是实现您想要的效果的好方法。

如 cmets 中所述,以这种方式连接 SQL 查询是等待发生的 SQL 注入,因此请使用数据库的参数化查询功能来防止这种情况。但是,我将按原样保留答案,因为它直接回答了所提出的问题。

【讨论】:

  • @Pablo 你甚至可以在,之后添加cmets
  • 另一种格式化此字符串的方法是在右括号后添加.format(...)% 格式化符号也必须工作,但我还没有尝试过
  • 请注意,每一行必须以字符串常量结尾,所以' foo '+variable 不起作用,但' foo '+variable+'' 会。
  • 此示例为 SQL 注入攻击打开了大门。请不要在任何面向公众的应用程序上使用它。有关如何使用“占位符”的信息,请参阅 MySQL 文档:dev.mysql.com/doc/connector-python/en/…
  • @Crossfit_and_Beer 它们称为查询参数,具有它们的查询称为参数化查询。每个主要的关系 DBMS 都支持它们。
【解决方案3】:

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

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 会心脏病发作
【解决方案4】:

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

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

【讨论】:

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

我发现在构建长字符串时,您通常会做一些类似构建 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、语法高亮等。但是,一些缺少空白的错误可能不会以这种方式被捕获。我建议,利用所有合理的优势来防范错误。
【解决方案6】:

这种方法使用:

  • 使用三重引号字符串几乎没有内部标点符号
  • 使用inspect 模块去除局部缩进
  • account_iddef_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 查询。从其他cmets,最好使用cursor.execute而不是dev.mysql.com/doc/connector-python/en/…
【解决方案7】:

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

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 似乎更自然、更容易。
【解决方案8】:

在 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-string 将如何工作?
  • 仍然容易受到 SQL 注入的攻击
【解决方案9】:

例如:

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

【讨论】:

    【解决方案10】:

    我发现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,则不需要反斜杠。
    【解决方案11】:

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

    对每个片段的评论:

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

    继续后不允许评论:

    使用反斜杠续行符 (\) 时,不允许使用 cmets。您将收到 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
    

    为正则表达式字符串提供更好的 cmets:

    基于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)
    

    【讨论】:

      【解决方案12】:

      作为在 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() 函数使用的。

      【讨论】:

        【解决方案13】:

        我个人认为以下是在 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} {1} {2}”然后更改最后一个来避免问号混淆线到cursor.execute(query.format(vars))。那应该照顾你唯一的“骗局”(我希望)。
        • 是的,使用format 会很好,但我不确定以这种方式格式化的查询字符串是否可以避免 SQL 注入。
        • 是的,这是一个公平的观点,它肯定会变得有点棘手。也许在完全可消耗的东西上测试它是明智的......毫无疑问是一个比较。科学。本科生很快就会过去。 ;)
        • @Ben 如果你这样做了cursor.execute(query.format(vars)),你将不再从准备好的语句中获利,所以你很容易受到许多问题的影响,首先是如果参数不仅仅是数字,你需要加倍在 SQL 查询中引用它们。
        【解决方案14】:

        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
        

        【讨论】:

          【解决方案15】:

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

          【讨论】:

            【解决方案16】:

            我通常使用这样的东西:

            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 稍微好一点(关于文本是否与开引号出现在同一行,不需要明确的行继续字符)。
            【解决方案17】:

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

            多行字符串有三引号:

            string = """line
              line2
              line3"""
            

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

            【讨论】:

              【解决方案18】:

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

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

              【讨论】:

                【解决方案19】:

                结合来自以下方面的想法:

                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] 的方式!” - ...

                【讨论】:

                  【解决方案20】:

                  我知道这是一个相当老的问题,但与此同时 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")
                  

                  【讨论】:

                    【解决方案21】:

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

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

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

                    【讨论】:

                      【解决方案22】:

                      "À 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            | ',' ')`.
                      

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

                      【讨论】:

                        【解决方案23】:

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

                        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
                        

                        【讨论】:

                          【解决方案24】:

                          我使用递归函数来构建复杂的 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)
                          

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

                          【讨论】:

                          • “递归函数”不就是叫lambda吗?
                          【解决方案25】:

                          来自official Python documentation

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

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

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

                          【讨论】:

                            【解决方案26】:

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

                            LONG_STRING = \
                            """
                            This is a long sting
                            that contains newlines.
                            The newlines are important.
                            """
                            
                            my_dict = {
                               'foo': 'bar',
                               'string': LONG_STRING
                            }
                            

                            【讨论】:

                              【解决方案27】:

                              我喜欢这种方法,因为它具有阅读权限。在我们有长字符串的情况下,没有办法!根据您所处的缩进级别,仍然限制为每行 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
                              )
                              """
                              

                              【讨论】:

                                【解决方案28】:

                                创建字符串

                                Python 字符串是通过将字符串的内容分配给写在双引号或单引号内的变量来创建的

                                Str1 ="Ramesh"  
                                
                                Str2 ='Mahesh'
                                
                                print(Str1) #Ramesh
                                
                                print(Str2) #Mahesh
                                

                                索引字符串

                                使用索引方法提取字符串(字符)的内容很有帮助,其中 python 字符串索引从索引零开始(Python 字符串索引为零)

                                Example of extracting string using positive string indexing :
                                
                                Str1 = "Ramesh"
                                
                                Str1[0] = "R"
                                
                                Str1[1] = "a"
                                
                                Str1[2] = "m"
                                
                                Str1[3] = "e"
                                
                                Str1[4] = "s"
                                
                                Str1[5] = "h"
                                

                                使用负字符串索引提取字符串的示例:

                                Str1 = "Ramesh"
                                
                                Str1[-6] = "R"
                                
                                Str1[-5] = "a"
                                
                                Str1[-4] = "m"
                                
                                Str1[-3] = "e"
                                
                                Str1[-2] = "s"
                                
                                Str1[-1] = "h"
                                

                                print(Str1[0]) #'R'

                                print(Str1[0:]) #'Ramesh'

                                print(Str1[2:]) #'mesh'

                                print(Str1[:6]) #'Ramesh'

                                print(Str1[-1:]) #'Ramesh'

                                创建多行长字符串的 Pythonic 方法

                                """Creating Python Comments. Python provides a single option to create a comment, using the pound sign, sometimes called the hash symbol
                                Python Multiline Comments with Consecutive Single-Line Comments
                                Python Multiline Comments with Multiline Strings
                                """
                                

                                【讨论】:

                                  【解决方案29】:

                                  嗯。

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

                                      # Suppose the following code is heavily indented
                                      line3header = "Third"
                                      variable = fr"""
                                  
                                  First line.
                                  Second line.
                                  {line3header} line.
                                  {{}} line.
                                  ...
                                  The last line.
                                  
                                      """
                                      variable = variable.strip()
                                      variable = variable.format("Fourth")
                                      variable += "\n"
                                  

                                  到此为止。

                                  【讨论】:

                                    【解决方案30】:

                                    一般情况下,我将listjoin 用于多行cmets/string。

                                    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”(换行符)或“,”(逗号)或“ ”(空格)。

                                    【讨论】:

                                    猜你喜欢
                                    • 2013-09-02
                                    • 2014-10-27
                                    • 1970-01-01
                                    • 2023-03-05
                                    • 2012-02-13
                                    相关资源
                                    最近更新 更多