避免在字符串中转义双引号

bla*_*ird 3 python string escaping

是否有Python的组件允许我绕过中间引号?在中,您是否可以指定主启动和停止打印调用,以便解释主启动和停止之间的所有内容,无论该元素最初代表什么?

我试图在程序中打印这行有趣的ASCII,这只是由于中间引号弹出而导致错误的行之一:

print"           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\."
                                                                      ^
SyntaxError: EOL while scanning string literal
Run Code Online (Sandbox Code Playgroud)

编辑:在考虑字符串文字的原始解释时,如果三重引号出现在您的行中,您还可以在原始解释中遇到三重引用的退出.

iCo*_*dez 5

为什么不使用三引号字符串有"""在每一端?

>>> print """           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\."""
           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\.
>>>
Run Code Online (Sandbox Code Playgroud)

请注意,您仍然需要转义字符串中与每端匹配的三重引号:

>>> print """ \""" """
 """
>>>
Run Code Online (Sandbox Code Playgroud)


Aar*_*all 5

另一种方法是简单地将这些行放入纯文本文件中,然后读取它们,就像我在 Linux/Unix 中所做的那样:

$ cat > my_file.txt
           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\.
^D <- control-d means end of file input from the command line
Run Code Online (Sandbox Code Playgroud)

然后使用Python:

with open('/path/my_file.txt') as f:
    print f.read()
Run Code Online (Sandbox Code Playgroud)

应该输出:

           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\.
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

1048 次

最近记录:

12 年,3 月 前