如何清除CSV文件中的整行并将详细信息移动到底部?

问题描述:

我正在做一个测验的登录系统。如果用户已经注册但未参加测验,即使他的登录详细信息位于中间,他也应该能够登录并开始测验。 所以这里是CSV:enter image description here如何清除CSV文件中的整行并将详细信息移动到底部?

这里是代码

`def login(): 
    found = 0 
    signed = 0 
    while found != True: 
     username = input("Please enter your username: ") 
     password = input("Please enter your password: ") 
     file=open("Data.csv","r") 
     for line in file: 
      details=line.split(",") 
      if details[0] == username and details[1] == password: 
       if details[4] == ('Teacher'): 
        signed = 2 #teacher log in 
        print('Teaacher login') 
       else: 
        if not details[11]: #row 11 is total score 
         signed = 1 #quiz not taken 
        else: 
         signed = 3 #quiz taken 
       found = True 
     if found == False: 
      print("Incorrect login details entered") 
    if signed == 3: 
     print('You have already taken a quiz') 
     quit() 
    if signed == 1: 
     #move all the details to the last line 
    return signed 

登录()`

,当我作为Gre17登录密码为QWERTY键盘,代码应该将他的细节。

moved 然后开始测验(不要担心)。 quiz taken 如何让这些细节被移动?

+0

不要使用'str.split()'来解析CSV - CSV比这更复杂。使用'csv'模块。 – Tomalak

+0

*“代码应该移动他的详细信息”* - 为什么?它是如何解决在CSV中移动的问题? – Tomalak

+0

那么如果有一种方法可以将他的分数添加到他们的底部,那么该如何呢? – 20PeterBread01

我认为,如果你想使用CSV做到这一点我觉得你可以先阅读该文件并存储详细的清单列表中的更好的使用MySQL或postgre为this.But数据库= file.readlines() 。更新所需的列表值并将列表写入文件。