这是解释这个问题的最简单方法.这是我正在使用的:
re.split('\W', 'foo/bar spam\neggs')
-> ['foo', 'bar', 'spam', 'eggs']
Run Code Online (Sandbox Code Playgroud)
这就是我想要的:
someMethod('\W', 'foo/bar spam\neggs')
-> ['foo', '/', 'bar', ' ', 'spam', '\n', 'eggs']
Run Code Online (Sandbox Code Playgroud)
原因是我想将一个字符串拆分成标记,操纵它,然后再将它重新组合在一起.
我在Python中收到很多这样的警告:
DeprecationWarning: invalid escape sequence \A
orcid_regex = '\A[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]\Z'
DeprecationWarning: invalid escape sequence \/
AUTH_TOKEN_PATH_PATTERN = '^\/api\/groups'
DeprecationWarning: invalid escape sequence \
"""
DeprecationWarning: invalid escape sequence \.
DOI_PATTERN = re.compile('(https?://(dx\.)?doi\.org/)?10\.[0-9]{4,}[.0-9]*/.*')
<unknown>:20: DeprecationWarning: invalid escape sequence \(
<unknown>:21: DeprecationWarning: invalid escape sequence \(
Run Code Online (Sandbox Code Playgroud)
他们的意思是什么?我该如何解决它们?
我有以下字符串
mystr1 = 'mydirname'
myfile = 'mydirname\myfilename'
Run Code Online (Sandbox Code Playgroud)
我正在努力做到这一点
newstr = re.sub(mystr1 + "\","",myfile)
Run Code Online (Sandbox Code Playgroud)
如何逃避我试图连接到mystr1的反斜杠?
在 Python 3.6.5 中,这可以正常工作:
command = "ffmpeg -i {0} -vsync 0 -q:v 2 -vf select=\"eq(pict_type\,PICT_TYPE_I)\" -r 30 {1}/frame%03d.jpg".format(file_path, output_path)
Run Code Online (Sandbox Code Playgroud)
这显然是一条长线,所以我使用了一个续行:
command = "ffmpeg -i {0} -vsync 0 -q:v 2 -vf select=\"eq(pict_type\,PICT_TYPE_I)\" -r 30 {1}/frame%03d.jpg"\
.format(file_path, output_path)
Run Code Online (Sandbox Code Playgroud)
但是,在启动时,这会生成DeprecationWarning:
DeprecationWarning: invalid escape sequence \,
command = "ffmpeg -i {0} -vsync 0 -q:v 2 -vf select=\"eq(pict_type\,PICT_TYPE_I)\" -r 30 {1}/frame%03d.jpg"\
Run Code Online (Sandbox Code Playgroud)
这确实不是,但是:
command = "foo {0} bar {1}"\
.format(file_path, output_path)
Run Code Online (Sandbox Code Playgroud)
我在项目的其余部分使用行延续;没有一个结果DeprecationWarning。像这样的其他问题提到了这个警告,但没有提到我能找到的连续字符。
是什么导致了这个警告,为什么它只出现在这种非常狭窄的情况下?
编辑:这与行延续无关。仅在某些时候向我显示错误的原因与 Django 的runserver …