Python:我正在制作几个列表来跟踪数据库中的变量

时间:2013-01-30 22:31:44

标签: database list file-io while-loop

应该将文本文件中的每一行保存到一个列表中,并根据逗号将其拆分并将它们分类为多个不同的列表。在while循环中发生错误,指出索引超出范围。

lineCounter = 0
j = 0
file = open("savefile.txt","r") 
with open('savefile.txt', 'r') as f:
    string = [line.strip() for line in f]
for line in file:
    lineCounter += 1
while(j<lineCounter):
    tempList = string[j].split(',')
    firstName[j] = tempList[0]
    lastName[j] = tempList[1]
    postition[j] = tempList[2]
    department[j] = tempList[3]
    seniority[j] = tempList[4]
    vacationWeeks[j] = tempList[5]
    sickDays[j] = tempList[6]
    iD[j] = tempList[7]
    status[j] = tempList[8]
    j += 1

打印firstName file.close()#关闭文本文件

1 个答案:

答案 0 :(得分:0)

NVM的问题是该列表需要附加而不是替换。