Python错误代码分析:莫名的[TypeError: 'int' object is not iterable]

一项目运行时出现如下错误提示:

TypeError: 'int' object is not iterable
错误代码最终定位于自定义函数 modify_table1()  第9行:

cells[j].text = rec_list[i][j]
将此行代码修改如下,运行正常:

if isinstance(rec_list[i][j], int):
    field_value = str(rec_list[i][j])
else:
    field_value = rec_list[i][j]
cells[j].text = field_value

错误原因:数据类型不匹配。


错误代码(片断)如下:

def modify_table1(docx, rec_list, prime_list):
    # rec_list: [(name, aqi, grade, prime),]
    tb = docx.tables[0]
    prime_list = get_prime_list(rec_list)
    rec_count = len(rec_list)
    for i in range(1, rec_count): 
        cells = tb.row_cells(i)
        for j in range(3):
            cells[j].text = rec_list[i][j]
            cell_para1 = cells[j].paragraphs[0]
            cell_para1.alignment = WD_ALIGN_PARAGRAPH.CENTER
        cells[3].text = prime_list[i]
        cell_para1 = cells[3].paragraphs[0]
        cell_para1.alignment = WD_ALIGN_PARAGRAPH.CENTER
    return

调用函数:
def create_report(filename, rec_list):
    path = "byc.docx"
    docx = Document(path)
    prime_list = get_prime_list(rec_list)       # chinese prime
    modify_para1(docx, rec_list, prime_list[0])
    modify_table1(docx, rec_list, prime_list)
    docx.save(filename)
    return


你可能感兴趣的:(Python,错误代码,python,代码分析)