AttributeError: type object 'typing.re' has no attribute 'sub'
时间: 2023-11-15 15:04:59 浏览: 169
AttributeError: type object 'typing.re' has no attribute 'sub'是由于在使用typing.re.sub()方法时,re模块中并没有sub()方法导致的。typing模块是Python3中的一个类型提示模块,它提供了一些类型提示工具,但它并不是Python的标准库,而是第三方库。因此,如果你想使用typing模块中的re.sub()方法,你需要先导入re模块。下面是一个例子,演示了如何使用typing.re.sub()方法:
```python
import re
from typing import List
def replace_words(words: List[str], old: str, new: str) -> List[str]:
return [re.sub(old, new, word) for word in words]
words = ['hello', 'world', 'python']
new_words = replace_words(words, 'o', '0')
print(new_words)
```
相关问题
AttributeError: type object 'typing.re' has no attribute 'findall'
AttributeError: type object 'typing.re' has no attribute 'findall'是因为在typing模块的re类中没有findall属性。这个错误通常发生在尝试访问一个不存在的属性时。在这种情况下,可能是因为你错误地使用了typing.re.findall(),而实际上应该使用re.findall()。
以下是一个演示如何使用re模块的findall()函数来查找匹配的字符串的例子:
```python
import re
text = "Hello, my name is John. I live in New York."
pattern = r"\b\w+\b" # 匹配单词
matches = re.findall(pattern, text)
print(matches) # 输出:['Hello', 'my', 'name', 'is', 'John', 'I', 'live', 'in', 'New', 'York']
```
在这个例子中,我们使用re模块的findall()函数来查找text中所有匹配pattern的单词,并将结果存储在matches变量中。最后,我们打印出匹配的结果。
AttributeError: type object 'datetime.time' has no attribute 'sleep'
AttributeError: type object 'datetime.time' has no attribute 'sleep' 这个错误是因为 datetime.time 类并没有 sleep() 这个方法,而你尝试使用了它。datetime.time 是 Python 内置的表示时间的类,它只包含小时、分钟、秒和微秒,因此并不具备线程休眠的功能。
如果你需要进行线程休眠的话,可以考虑使用 time 模块下的 sleep() 函数,例如:time.sleep(1) 可以使当前线程休眠 1 秒钟。
阅读全文
相关推荐
















