TypeError: argument of type 'int' is not iterable
时间: 2023-10-27 20:05:58 浏览: 263
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 ‘int’ is not iterable
TypeError: "argument of type 'int' is not iterable" 这是一个Python编程错误,它的意思是你正在尝试对一个整数(int)类型的值进行迭代操作,而整数是不可迭代的。在Python中,只有序列类型(列表、元组、字符串、字典等)或者集合等可迭代容器类型是可以迭代的。
举个例子,当你像下面这样操作:
```python
list_of_numbers = [1, 2, 3]
for num in 5: # 正确做法
print(num)
# 错误示例:
num = 5
for i in num: # TypeError会在这里抛出,因为5是整数,不是一个可迭代的对象
print(i)
```
在第二段代码中,试图通过 `for i in num:` 遍历整数5,由于整数不是可迭代的,所以会引发这个错误。
要解决这个问题,你需要确保你的迭代作用于一个可迭代的对象上。如果想遍历数字,应该创建一个包含这个数字的列表或其他可迭代类型:
```python
num = 5
numbers = [num] # 创建一个只包含5的列表
for i in numbers:
print(i)
```
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)
```
这样就可以正常运行了。
阅读全文