简体   繁体   English

多行字符串文字解析

[英]Multiline string literal parsing

I have tried the following in Python's Interactive console: 我在Python的交互式控制台中尝试了以下内容:

>>> """"string"""
'"string'
>>> """"string""""
SyntaxError: EOL while scanning string literal

I expect the latter case """"string"""" to return '"string"' because I have three quotes at the start and three quotes at the end. 我希望后一种情况""""string""""返回'"string"'因为我在开头有三个引号,在结尾有三个引号。 How does Python interpret it? Python如何解释它?

Python is interpreting it as: Python将其解释为:

 """"string""" "                                                                                                                   "
#^^^These three " to start the string literal. The next one counts in the string.
#The three last ones after the last one are counted as the end.

Notice the straying " . 注意偏离"

You can just do: 你可以这样做:

'''"string"'''

It sees the triple-quoted string """"string""" , followed by a non-triple-quoted string that doesn't complete by EOL, " . 它看到三重引用的字符串""""string""" ,后跟一个非三重引用的字符串,它不能通过EOL完成, "

The tokenize module can show you what it's doing: tokenize模块可以显示它正在做什么:

s = '""""string""""'
g = tokenize.generate_tokens(io.StringIO(s).readline)
t = list(g)
print(t)

This prints a STRING token with '""""string"""' , then an ERRORTOKEN token with '"' . 这将打印带有'""""string"""'的STRING标记,然后是带有'"'的ERRORTOKEN标记。

In general, the best way of answering any question like this when you can't figure out how to interpret the grammar (I assume you looked at the grammar first?) is to use tokenize, ast, and friends. 一般来说,当你无法弄清楚如何解释语法时 (我假设你先看了语法?),回答任何问题的最好方法是使用tokenize,ast和friends。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM