if cell.value != 'None': AttributeError: 'str' object has no attribute 'value'
时间: 2023-11-10 18:06:01 浏览: 43
这个错误提示是因为你在一个字符串对象上调用了 `value` 属性,而字符串对象并没有这个属性。这个错误通常出现在使用类似于 openpyxl 这样的库读取 Excel 文件时,当读取到空单元格时,返回的是一个字符串类型的 'None',而不是 Python 内置的 NoneType 类型。
为了避免这个错误,你可以在读取单元格的值之前,先判断该单元格是否为空,例如:
```python
if cell.value is not None:
# do something with cell value
```
相关问题
if table3.cell(i,j).text.blod==True: AttributeError: 'str' object has no attribute 'blod'
这个错误是因为你在一个字符串对象上使用了`blod`属性,但是字符串对象没有`blod`属性。实际上,你需要使用`python-docx`库中的`Text`对象来检查文本的粗体属性。以下是一个修改后的例子,演示如何在一个Word表格中查找粗体字:
```python
from docx import Document
# 打开Word文档
doc = Document('example.docx')
# 获取第三个表格
table3 = doc.tables[2]
# 遍历表格中的所有单元格
for i in range(len(table3.rows)):
for j in range(len(table3.columns)):
# 获取单元格文本
cell_text = table3.cell(i,j).text
# 检查文本是否为粗体
for paragraph in table3.cell(i,j).paragraphs:
for run in paragraph.runs:
if run.bold:
print(f"Bold text found in cell ({i},{j}): '{cell_text}'")
```
在这个例子中,我们首先获取了文档中的第三个表格,并遍历表格中的所有单元格。对于每个单元格,我们获取单元格中的文本,并使用`runs`属性检查文本中是否存在粗体字。注意,我们需要遍历单元格中的所有paragraphs和runs来检查文本的粗体属性。如果找到粗体字,则打印出单元格的行列和文本。
elif skipped.find(depend) != 0: AttributeError: 'int' object has no attribute 'find'
这个错误是因为skipped是一个整数,而不是字符串,因此无法使用字符串方法find()。在这段代码中,可能是因为想要判断depend是否在skipped中,但是使用了错误的方法。
要解决这个错误,可以先将skipped转换为字符串,然后再使用find()方法判断depend是否在skipped中。具体实现可以使用str()函数将skipped转换为字符串,如下所示:
```python
skipped = self._outcome.result.skipped
skipped_str = str(skipped)
if skipped_str.find(depend) != -1:
# do something
else:
# do something else
```
注意,这里使用了find()方法判断depend是否在skipped中时,应该判断是否等于-1,而不是不等于0。因为find()方法返回的是查找到的字符串的索引,如果未找到,则返回-1。如果使用不等于0来判断,那么如果depend在字符串的第一个位置,会被误判为未找到。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)