简体   繁体   English

从文本文件到列表框的偶数行-Python

[英]Even numbered lines from a text file to a listbox - Python

I have a username-password login system and would like to add a delete user function. 我有一个用户名密码登录系统,想添加一个删除用户功能。 The usernames are stored on the even lines of a text file (inc zero) and the passwords on odd numbered lines. 用户名存储在文本文件的偶数行(增量为零)上,密码存储在奇数行上。 Is there a way of adding only the even numbered lines to a listbox to be selected via curselection to be deleted as showing the passwords would be against the point. 有没有一种方法可以将仅偶数行添加到要通过curselection进行选择的列表框中,以删除密码,因为这表明密码是不对的。 I currently have all of the contents of the text file showing in the listbox however would like only the usernames to be shown. 我目前在列表框中显示了文本文件的所有内容,但是只希望显示用户名。 To delete the lines I have copied the contents of the listbox to a new text file after removing the chosen line from the listbox. 为了删除行,在从列表框中删除所选行之后,我已将列表框的内容复制到了一个新的文本文件中。 Would there also be a way to delete the corresponding password after the username to be deleted has been selected ? 选择了要删除的用户名后,还有没有办法删除相应的密码?

def DelUser():

    global rootDU
    global listbox

    rootDU = Tk()
    rootDU.title('Delete user')

    users = open(creds, 'r')
    mylist = users.readlines()
    users.close()

    listbox = Listbox(rootDU, width=50, height=6)
    listbox.grid(row=0, column=0)
    yscroll = Scrollbar(command=listbox.yview, orient=VERTICAL)
    yscroll.grid(row=0, column=1, sticky=N + S)
    listbox.configure(yscrollcommand=yscroll.set)

    enter1 = Label(rootDU, text='Click on the user to delete', width=50)
    enter1.grid(row=1, column=0)

    for item in mylist:
        listbox.insert(END, item)

    delButton = Button(rootDU, text='Delete', command=RemoveUser)
    delButton.grid(columnspan=2, sticky=W)
    delButton.grid(row=2, column=0)

def RemoveUser():

    global listbox

    try:
        index = listbox.curselection()[0]
        listbox.delete(index)
        os.remove(creds)
        newfile = 'tempfile.txt'
        with open(newfile, 'w') as f:
            f.write(''.join(listbox.get(0, END)))
            f.close()

    except IndexError:
        pass

Just add only every second line to your listbox: 只需将第二行添加到您的列表框中:

for item in mylist[::2]:
    listbox.insert(END, item)

The ::2 slice picks every second line, starting at line 0. See Explain Python's slice notation for further details. ::2切片每隔第二行从第0行开始进行选择。有关更多详细信息,请参见解释Python的切片符号

You then still have to re-read the file to find the corresponding password however, since you don't store that information separately. 然后,由于您没有单独存储该信息,因此您仍然必须重新读取该文件以找到相应的密码。 You really want to store a mapping from usernames to passwords (separately, as information you share between DelUser and RemoveUser ) so you can easily re-write your user file with the remaining users and passwords. 您确实要存储从用户名到密码的映射(作为单独的信息,作为您在DelUserRemoveUser之间共享的信息),以便您可以轻松地用其余用户和密码来重写用户文件。

You could also consider using a different format for your file; 您也可以考虑使用其他格式的文件; writing usernames and passwords on the same line , but putting a delimiter character between them, would make it easier to process the data. 在同一行上写入用户名和密码,但在它们之间放置定界符,将使处理数据更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM