'str' object has no attribute findall
时间: 2023-10-05 17:09:25 浏览: 105
The `findall` method is not available for `str` objects in Python. It is only available for `re` module's regular expression objects. If you want to find all occurrences of a pattern in a string, you can use the `re.findall()` function instead. Here's an example:
```python
import re
text = "Hello, how are you? Are you doing well?"
pattern = r"\b\w{3}\b" # Matches three-letter words
matches = re.findall(pattern, text)
print(matches)
```
This will output `['how', 'are', 'you', 'you']`, which are all the three-letter words found in the given text.
相关问题
'str' object has no attribute 'findall'
这个错误通常是因为你尝试在字符串上调用 `findall` 方法,但是字符串类型并没有这个方法。`findall` 是正则表达式模块 `re` 中的方法,你需要先导入 `re` 模块,然后使用 `re.findall()` 方法来进行匹配。
例如,如果你想在一个字符串中查找所有的数字,可以这样写:
```python
import re
s = "I have 3 apples and 5 oranges"
numbers = re.findall(r'\d+', s)
print(numbers) # ['3', '5']
```
AttributeError: 'str' object has no attribute 'findall'
引用的赋值改为d[b]=[word。这样就能修复这个错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [python基础教程(第三版)学习笔记(九)](https://blog.csdn.net/micorjun/article/details/83690418)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [Python面向对象编程](https://blog.csdn.net/weixin_47692652/article/details/128294400)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [python自学日记9——选择数据结构](https://blog.csdn.net/zxbylx1120471286/article/details/102632366)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文