在python 3.2中创建具有特定文本文件数据的列

时间:2017-07-05 12:27:31

标签: python

这是为了询问用户的用户名,在文本文件中找到用户名,然后在列中输出,只输出他们的信息。信息包括身份证,姓氏,加入年份,状态,预订的夜晚和积分。我该怎么做???

import time
membr = int(input("Do you have a membership already?\n"
                  "1.Yes\n"
                  "2.No\n"
                  "Option: "))

if membr == 1:
    MemberID = []
    LastName = []
    YearJoined = []
    Status = []
    NightsBooked = []
    Points = []

    theirid = input("Please enter your id number (It is case sensitive): ")
    Myfile = open("Memberships.txt", "r")
    x = 0
    for line in Myfile:
        if theirid in line: return(line)
        information = line.split(",")
        MemberID.append(information[0])
        LastName.append(information[1])
        YearJoined.append(information[2])
        Status.append(information[3])
        NightsBooked.append(information[4])
        Points.append(information[5])
        x = x+1
    Myfile.close()
    print("{0:<18} {1:<18} {2:<18} {3:<18} {4:<18} {5:<18}".format("MemberID", "LastName", "YearJoined", "Status", "NightsBooked", "Points"))
    for y in range(1,x):
        print("{0:<18} {1:<18} {2:<18} {3:<18} {4:<18} {5:<18}".format(MemberID[y],LastName[y],YearJoined[y],Status[y],NightsBooked[y],Points[y]))
    time.sleep(2)
    mainmenu()
elif membr == 2:
    createnewuser()
else:
    print("Invalid")
    time.sleep(2)
    mainmenu()

1 个答案:

答案 0 :(得分:0)

你不需要在那里归还那条线。

    ...
    YearJoined = []
    Status = []
    NightsBooked = []
    Points = []

    theirid = input("Please enter your id number (It is case sensitive): ")
    Myfile = open("Memberships.txt", "r")
    x = 0
    for line in Myfile:
        # this will use the line found to do your work.
        if theirid in line:
            information = line.split(",")
            MemberID.append(information[0])
            LastName.append(information[1])
            YearJoined.append(information[2])
            Status.append(information[3])
            NightsBooked.append(information[4])
            Points.append(information[5])
            x = x+1
    Myfile.close()
    print("{0:<18} {1:<18} {2:<18} {3:<18} {4:<18} {5:<18}".format("MemberID", "LastName", "YearJoined", "Status", "NightsBooked", "Points"))
    for y in range(1,x):
        print("{0:<18} {1:<18} {2:<18} {3:<18} {4:<18} {5:<18}".format(MemberID[y],LastName[y],YearJoined[y],Status[y],NightsBooked[y],Points[y]))
    time.sleep(2)
    mainmenu()
elif membr == 2:
    createnewuser()
else:
    print("Invalid")
    time.sleep(2)
    mainmenu()