Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中引用不带换行符的长字符串_Python - Fatal编程技术网

在Python中引用不带换行符的长字符串

在Python中引用不带换行符的长字符串,python,Python,我试图用Python编写一个长字符串,显示为OptParser选项的帮助项。在我的source code.py文件中,我希望放置新行,这样我的代码就不会花费新行。但是,我不希望这些换行符影响代码运行时字符串的显示方式。例如,我想写: parser.add_option("--my-option", dest="my_option", nargs=2, default=None, help='''Here is a long description of my o

我试图用Python编写一个长字符串,显示为OptParser选项的帮助项。在我的source code.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调用我的程序时,上面的方法会使我的选择有很多漏洞

我怎样才能解决这个问题

谢谢。

这项功能:

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

print foo

您可以像在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.") 

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

只要利用字符串文字并置的优势就可以了——在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
传递的每一段文字末尾的尾随空格:必须明确地将它们放在那里,否则并列将产生“单词”,例如
doesmany
等等;-)


请注意,与一些答案和评论所声称的非常不同,您在这里绝对不需要丑陋、多余的加号和/或反斜杠——不需要加号,因为编译器为您应用并置,也不需要反斜杠,因为这组物理行是单个逻辑行(由于在物理行集结束之前打开的参数尚未关闭!)。

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

>>> 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
要删除任何可用的公共前导空格,请执行以下操作:


我可以这样做,但那似乎很冗长……有没有一个标准的简写法呢?我基本上想要一些语法糖,让python像TeX一样处理字符串,其中换行空格不重要。您仍然需要在行的末尾加上\号。@Emil H:没有,因为它包含在
()
作为参数的一部分。@Emil,否:这里绝对不需要任何反斜杠——否则的话就大错特错了!Python编译器在多个物理行中生成一个逻辑like,因为第一个物理行上的开括号尚未闭合!
>>> 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.