在写re.compile
时,我多次使用r
前缀(re.compile(r'(xyx)
)。但是,我第一次看到(re.compile(f'(xyx)
),我不确定它在做什么。输出对我来说也没有任何意义。有人能解释一下这个f
在这里做什么吗?在
import re, string
re_tok = re.compile(f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])')
def tokenize(s):
return re_tok.sub(r' \1 ', s).split()
>>> tokenize('˚∆˙©∆©˙¬ ldgkl slgh lshsg ieh 954n bvery590oerfdb o3pg')
格式化字符串文本或f-string是以“f”或“f”为前缀的字符串文本。在
这些字符串可能包含替换字段,这些字段是用大括号{}分隔的表达式。在
虽然其他字符串文本始终具有常量值,但格式化字符串实际上是在运行时计算的表达式。在
在这种情况下:
'string.punctuation`` are a replacement field, i.e. the string is to be formatted with
周围的大括号字符串.标点符号, which, in Python, is a 'string of ASCII characters which are considered punctuation marks in the
C“语言环境”。在要了解更多信息,请查看thesePython文档和^{} references :-)
这些是修改字符串文字行为的各种标志
r
表示原始字符串,f
表示字符串插值参见政治公众人物的解释:
https://www.python.org/dev/peps/pep-0498/
以及python文档:
关于
r
https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
关于
f
https://docs.python.org/3/reference/lexical_analysis.html#f-strings
根据python documentation:
文档中有多个示例,因此我将发布其中几个示例并解释:
这里,}
!
引入了一个转换字段。!r
调用^{由于它是使用
^{pr2}$format()
协议格式化的,以下是其他用例:偶数日期时间对象:
根据上面的信息,我们将其应用到您的代码中:
上面的行将
string.punctuation
插入到该位置的字符串中。在According to the docs,
string.punctuation
是:如果您真的想深入了解这个问题:什么是
C
语言环境?在相关问题 更多 >
编程相关推荐