gpt4 book ai didi

Python 重新 "bogus escape error"

转载 作者:太空狗 更新时间:2023-10-29 18:14:17 25 4
gpt4 key购买 nike

我一直在摆弄 python re modules .search 方法。 cur 是来自 Tkinter 条目小部件的输入。每当我在条目小部件中输入“\”时,它都会抛出此错误。我不确定错误是什么或如何处理它。任何见解将不胜感激。

cur 是一个字符串

tup[0]也是一个字符串

片段:

se = re.search(cur, tup[0], flags=re.IGNORECASE)

错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python26\Lib\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Python26\Suite\quidgets7.py", line 2874, in quick_links_results
self.quick_links_results_s()
File "C:\Python26\Suite\quidgets7.py", line 2893, in quick_links_results_s
se = re.search(cur, tup[0], flags=re.IGNORECASE)
File "C:\Python26\Lib\re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "C:\Python26\Lib\re.py", line 245, in _compile
raise error, v # invalid expression
error: bogus escape (end of line)

最佳答案

“虚假转义(行尾)”意味着您的模式以反斜杠结尾。这与 Tkinter 无关。您可以在交互式 shell 中很容易地复制错误:

>>> import re
>>> pattern="foobar\\"
>>> re.search(pattern, "foobar")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 241, in _compile
raise error, v # invalid expression
sre_constants.error: bogus escape (end of line)

解决方案?确保您的模式不以单个反斜杠结尾。

关于Python 重新 "bogus escape error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4427174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com