在Python中引用没有换行符的长字符串

时间:2010-06-19 19:45:10

标签: python

我正在尝试在Python中编写一个长字符串,该字符串显示为OptParser选项的帮助项。在我的源代码.py文件中,我想放置换行符,以便我的代码不会花费新行。但是,我不希望这些换行符影响代码运行时该字符串的显示方式。例如,我想写:

   parser.add_option("--my-option", dest="my_option", nargs=2, default=None,
              help='''Here is a long description of my option.  It does many things
              but I want the shell to decide how to display this explanation. However,
              I want newlines in this string.''')

上面的做事方式会让我这样做 - 当我用--help调用我的程序时,my-option的解释会有很多空白。

我该如何解决这个问题?

感谢。

4 个答案:

答案 0 :(得分:18)

您可以像在C中一样连接字符串文字,因此"foo" "bar""foobar"相同,这意味着这应该符合您的要求:

parser.add_option("--my-option", dest="my_option", nargs=2, default=None, 
          help="Here is a long description of my option.  It does many things "
          "but I want the shell to decide how to display this explanation. However, "
          "I want newlines in this string.") 

请注意,您不需要反斜杠行继续符,因为整个内容都在括号内。

答案 1 :(得分:9)

只是利用字符串文字并置 - 在Python中,就像在C中一样,如果两个字符串文字彼此相邻而中间只有空格(包括换行符),编译器将将它们合并为单个字符串文字,忽略空格。即:

parser.add_option("--my-option", dest="my_option", nargs=2, default=None,
                  help='Here is a long description of my option.  It does '
                       'many things but I want the shell to decide how to '
                       'display this explanation. However, I do NOT want '
                       'newlines in this string.')

我假设“我想要”你的意思是“我不想要”这里; - )。

请注意您传递的文字的每一段末尾的尾随空格为help:您必须明确地将它们放在那里,否则并置会产生诸如{{1}之类的“单词”等等; - )。

请注意,与一些答案和评论所声称的完全不同,你绝对需要丑陋,冗余的加号和/或反斜杠 - 不需要加号,因为编译器应用了并列你,不需要反斜杠,因为这组物理线是一条逻辑线(感谢开放的paren没有被关闭,直到物理线组结束!)。

答案 2 :(得分:4)

默认帮助格式化程序重新格式化字符串,以便您可以根据需要在帮助字符串中使用换行符:

>>> from optparse import OptionParser
>>> parser = OptionParser()
>>> parser.add_option('--my-option', help='''aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaa
... b
... c                            d
... e
... f''')
>>> parser.print_help()
Usage: bpython [options]

Options:
  -h, --help            show this help message and exit
  --my-option=MY_OPTION
                        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                        aaa b c                            d e f

要移除任何常用的领先空间,您可以使用textwrap.dedent

>>> from optparse import OptionParser
>>> from textwrap import dedent
>>> parser = OptionParser()
>>> parser.add_option('--my-option', help=dedent('''\
...     Here is a long description of my option.  It does many things
...     but I want the shell to decide how to display this
...     explanation. However, I want newlines in this string.'''))
>>> parser.print_help()
Usage:  [options]

Options:
  -h, --help            show this help message and exit
  --my-option=MY_OPTION
                        Here is a long description of my option.  It does many
                        things but I want the shell to decide how to display
                        this explanation. However, I want newlines in this
                        string.

答案 3 :(得分:1)

这有效:

foo = "abc" + \
    "def" + \
    "ghi"

print foo