如何修复“<string>"弃用警告:转义序列无效"在 Python 中? [英] How to fix &quot;&lt;string&gt; DeprecationWarning: invalid escape sequence&quot; in Python?

查看:101
本文介绍了如何修复“<string>"弃用警告:转义序列无效"在 Python 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中收到很多这样的警告:

DeprecationWarning: 无效的转义序列 \Aorcid_regex = '\A[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]\Z'弃用警告:转义序列无效 \/AUTH_TOKEN_PATH_PATTERN = '^\/api\/groups'弃用警告:转义序列无效 \"""弃用警告:转义序列无效\.DOI_PATTERN = re.compile('(https?://(dx\.)?doi\.org/)?10\.[0-9]{4,}[.0-9]*/.*')<unknown>:20: DeprecationWarning: 无效的转义序列 \(<unknown>:21: DeprecationWarning: 无效的转义序列 \(

它们是什么意思?我该如何修复它们?

解决方案

\ 是 Python 字符串文字中的转义字符.

例如,如果您想在字符串中放置一个制表符,您可以这样做:

<预><代码>>>>打印(foo \t bar")富吧

如果你想把文字 \ 放在一个字符串中,你必须使用 \\:

<预><代码>>>>打印(foo \\ bar")foo\bar

或者使用原始字符串":

<预><代码>>>>打印(rfoo \ bar")foo\bar

你不能在任何需要的时候就在字符串文字中加入反斜杠.当后面没有有效转义序列之一时,反斜杠无效,并且 较新版本的 Python 会打印弃用警告.例如 \A 不是转义序列:

$ python3.6 -Wd -c '"\A"'<string>:1: DeprecationWarning: 无效的转义序列\A

如果您的反斜杠序列不小心与 Python 的转义序列之一匹配,但您不是故意的,那就更糟了.

所以你应该总是使用原始字符串或 \\.

记住字符串文字仍然是字符串文字很重要,即使该字符串旨在用作正则表达式.Python 的正则表达式语法支持许多以 \<开头的特殊序列/代码>.例如 \A 匹配字符串的开头.但是 \A 在 Python 字符串文字中无效!这是无效的:

my_regex = "\Afoo"

相反,您应该这样做:

my_regex = r"\Afoo"

文档字符串是另一个需要记住的:文档字符串也是字符串文字,无效的 \ 序列在文档字符串中也是无效的!如果文档字符串包含 \ 的,请使用原始字符串 (r"""...""").

I'm getting lots of warnings like this in 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 \(

What do they mean? And how can I fix them?

解决方案

\ is the escape character in Python string literals.

For example if you want to put a tab character in a string you would do:

>>> print("foo \t bar")
foo      bar

If you want to put a literal \ in a string you have to use \\:

>>> print("foo \\ bar")
foo \ bar

Or use a "raw string":

>>> print(r"foo \ bar")
foo \ bar

You can't just go putting backslashes in string literals whenever you want one. A backslash isn't valid when not followed by one of the valid escape sequences, and newer versions of Python print a deprecation warning. For example \A isn't an escape sequence:

$ python3.6 -Wd -c '"\A"'
<string>:1: DeprecationWarning: invalid escape sequence \A

If your backslash sequence does accidentally match one of Python's escape sequences, but you didn't mean it to, that's even worse.

So you should always use raw strings or \\.

It's important to remember that a string literal is still a string literal even if that string is intended to be used as a regular expression. Python's regular expression syntax supports lots of special sequences that begin with \. For example \A matches the start of a string. But \A is not valid in a Python string literal! This is invalid:

my_regex = "\Afoo"

Instead you should do this:

my_regex = r"\Afoo"

Docstrings are another one to remember: docstrings are string literals too, and invalid \ sequences are invalid in docstrings too! Use raw strings (r"""...""") for docstrings if they contain \'s.

这篇关于如何修复“<string>"弃用警告:转义序列无效"在 Python 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆