TypeError: argument of type 'int' is not iterable
时间: 2023-10-27 15:05:58 浏览: 54
This error occurs when you try to iterate over an integer value, which is not possible in Python.
For example, if you have the following code:
```
num = 10
for i in num:
print(i)
```
You will get the TypeError: argument of type 'int' is not iterable because you are trying to iterate over an integer value 'num'.
To fix this error, you need to make sure that you are iterating over an iterable object, such as a list or a tuple. For example:
```
nums = [1, 2, 3, 4, 5]
for num in nums:
print(num)
```
Here, we are iterating over the list 'nums', which is an iterable object, so we can successfully iterate over the values in the list without getting the TypeError.
相关问题
TypeError: argument of type 'numpy.int64' is not iterable
这个错误通常会出现在使用NumPy中的某些函数时,因为该函数需要一个可迭代的对象作为输入参数,但输入了一个整数或其他不可迭代的对象。
可能的解决方案:
1. 检查代码中使用的所有NumPy函数,并确保传递给它们的参数是可迭代的对象。
2. 如果输入参数是整数,可以将其转换为列表或数组,以便可以进行迭代。例如,可以使用`[number]`将整数转换为包含该整数的列表。
3. 如果您不确定哪个函数导致了错误,请逐步调试代码并打印变量以查找问题。
if 'S6.4.1' in row[0].value: TypeError: argument of type 'int' is not iterable
抱歉,这是我的失误。在上面的代码中,`row[0].value`是一个字符串,因此应该使用`in`来判断字符串是否包含目标子串。在修改后的代码中,我们应该使用`str(row[0].value)`将该单元格的值转换为字符串,然后再对其进行判断。
以下是修改后的代码:
```python
from openpyxl import load_workbook
# 打开Excel文件
wb = load_workbook(filename='C_Geely_Lotus DMS 20230606.xlsx')
# 选择Release Matrix-Lambda表格
ws = wb['Release Matrix-Lambda']
# 遍历每一行
for row in ws.iter_rows(min_row=2):
# 判断第一列是否包含目标字符串
if 'S6.4.1' in str(row[0].value):
# 输出对应的行数
print(row[0].row)
```
这样就可以正常运行了。