我正在建立一个程序,该程序从CSV文件中读取数据,通过一连串的比较来运行该数据,然后告诉用户其数据是否通过了这些测试。但是我一直遇到一个问题,它只读取文件的最后一行
这是我的CSV文件:
1,Gregg,1,True,900,1800
2,Scott,2,False,0,200
3,Trish,3,True,900,1800
4,Rob,4,False,0,200
5,Mat,5,True,900,1800
6,Ben,6,False,0,200
7,Mason,7,True,900,1800
8,Ciaran,8,False,0,200
9,Aaron,9,True,900,1800
10,Dylan,10,False,0,200
11,Allistair,1,True,900,1800
12,Owen,2,False,0,200
13,Mateja,3,True,900,1800
14,Tom,4,False,0,200
15,David,5,True,900,1800
16,Aaron,6,False,0,200
17,Bjork,7,True,900,1800
18,Snorri,8,False,0,200
19,James,9,True,900,1800
20,Madison,5,False,0,200
这是我的记录结构(存储在record.py中):
class User:
# Create a record structure for the user, which will encompass and ID, name, level, presence indicator, opentime and closetime
def __init__(self, usrID, name, lvl, present, opentime, closetime):
self.usrID = usrID
self.name = name
self.lvl = int(lvl)
self.present = bool(present)
self.opentime = opentime
self.closetime = closetime
# return a string of text when the User class is called
def __repr__(self):
if self.present == True:
return "User with ID " + self.usrID + ", with name " + self.name + " has level " + str(self.lvl) + " and is present in the building"
else:
return "User with ID " + self.usrID + ", with name " + self.name + " has level " + str(self.lvl) + " and isn't present in the building"
这是我的代码:
def readusrs():
# define f to open testaccounts.csv
f = open('csv/users.csv')
# open testaccounts.csv in csvreader mode
csvFile = csv.reader(f)
# reset out counter
i = 0
# for every row in the csv file, populate our arrays
for row in csvFile:
usrs[i].usrID = row[0]
usrs[i].name = row[1]
usrs[i].plvl = (row[2])
usrs[i].present = bool(row[3])
usrs[i].oentime = row[4]
usrs[i].closetime = row[5]
# incrament i
i = i + 1
# return all of the account data
return usrs
usrs = [record.User("", "", 0, True, "", "")] * 20
usrs = readusrs()
我希望程序中的数据像CSV文件中那样出来,但是全部都像这样:
ID为20的用户(名称为Madison)的级别为0,并且存在于建筑物中