这是我的代码:
for i in range(1, maxRows+1):
nameContent = str(sheet.cell(row = i, column = 1).value)
nameList = []
nameList.append(nameContent)
print(nameList)
rateContent = float(sheet.cell(row = i, column = 3).value)
rateList = []
rateList.append(rateContent)
print(rateList)
hoursContent = float(sheet.cell(row = i, column = 2).value)
hoursList = []
hoursList.append(hoursContent)
print(hoursList)
当我打印每个列表时,它仅打印最新的文本/值。如何将所有值/文本保留在列表中,以便可以在以后的代码中使用列表?
注意:我正在使用openpyxl模块
答案 0 :(得分:1)
您正在重新定义循环内的列表。这意味着在每次迭代时,它将列表重置为空列表。尝试将列表定义(namelist = []
)放入循环之外。