tuple 和 int 的区别,错误不可排序类型:tuple() < int()?

Difference between tuple and int, and error unorderable types: tuple() < int()?

我收到一个我无法正确理解的错误。即根据错误 tupleint 之间的差异。我读了一个答案,但从初学者(没有编码背景)的角度来看并不能真正理解它。

Traceback (most recent call last): File "", line 7, in File "C:\Python34\lib\site-packages\openpyxl\worksheet\worksheet.py", line 300, in cell if row < 1 or column < 1: TypeError: unorderable types: tuple() < int()

代码:

import re, openpyxl, os, sys
from openpyxl import Workbook
from openpyxl.compat import range
from openpyxl.utils import get_column_letter

cont_us = ['hi','how','are']
wb = Workbook()
dest_filename = "Trying web.xlsx"
ws1 = wb.active
if cont_us:
    i=1
    for i in enumerate(cont_us,0):
        ws1.cell(row = i, column = 2).value = cont_us
        i = i+1
        wb.save(file_name = dest_filename)

enumerate returns 包含索引和关联项的元组的可迭代对象。您应该相应地解压缩以获得可用于访问工作表中的单元格的整数索引(不是元组):

if cont_us:
    for i, x in enumerate(cont_us, 1):
        ws1.cell(row = i, column = 2).value = x # assign item, not list
wb.save(file_name = dest_filename)

此外,如果您不需要列表中的项目,您可以简单地使用 range:

if cont_us:
    for i in range(len(cont_us)):
        ...

附带说明一下,您不需要初始化或递增 ifor 循环已经做到了。