Python CSV文件:打印所有用户的详细信息

时间:2017-12-04 18:26:24

标签: python csv

我正在编写一段代码,其中我的用户进行测验,所有信息都存储在CSV文件中。然后,我在另一个程序中,让用户输入一个用户名,如果匹配,它将打印该用户的分数等。但是,我让同一个用户再次进行测验,而不是打印用户所做的所有测验和分数,而只打印最新的测验。

显示: 我的CSV文件看起来像这样

Ser15 A Sci-Med

Ser15 C Sci-Med

这些是行。我希望我的程序可以打印所有测验和成绩'Ser15',但它只是打印最新的。 如何让我的程序打印所有程序?这是我的代码......

     def Report1():
        found=False
        UserFind=input("Please enter a username")
        with open("Report.csv","r") as c:
            for row in c:
                details=row.split(",")
                if UserFind in details[0]:
                    found=True
                else:
                  pass
            if found==True:
                print(details[0])
                print(details[1])
                print(details[2])
                print("These are your user's details!")
            else:
                print("User doesn't exist or wrong details...")

这是我的结果:

Please enter a choice: 3
Please enter a usernameSer15
Ser15
C
Sci-Med

These are your user's details!

正如您所看到的那样,它会打印出最新信息,而非所有信息......

这是我项目的一部分,所以非常感谢任何帮助!

4 个答案:

答案 0 :(得分:0)

问题只是那个

        for row in c:
            details=row.split(",")
            if UserFind in details[0]:
                found=True

不会停止循环,因此您将获得最后一行中的所有内容。

只需在break之后添加found=True即可停止匹配的行

请注意,如果要执行多次搜索,则此线性搜索不是最佳选择。更好地用csv行构建字典:

import csv
with open("Report.csv","r") as c:
    cr=csv.reader(c)
    database = {row[0]:row[1:] for row in c}

现在使用database或/和name in database查询database.get(name)以获取其余信息。

答案 1 :(得分:0)

循环浏览CSV文件时,应检查是否找到它并在循环中打印。

with open("Report.csv","r") as c:
    for row in c:
        details=row.split(",")
        if UserFind in details[0]:
            found=True
            print(details[0])
            print(details[1])
            print(details[2])
            print("These are your user's details!")
    if not found:
        print("User doesn't exist or wrong details...")

希望这有帮助。

答案 2 :(得分:0)

你的打印语句不在for循环中,但在它之后。所以他们只运行一次。并且您检查found以决定是否打印是没有意义的:这将根据是否有任何行匹配打印,而不是当前行是否匹配。此外,没有必要else: pass。您应该将代码更改为以下内容:

def Report1():
    found=False
    UserFind=input("Please enter a username")
    with open("Report.csv","r") as c:
        for row in c:
            details=row.split(",")
            if UserFind in details[0]:
                print(details[0])
                print(details[1])
                print(details[2])
                print("These are your user's details!")
                found= True
        if not found:
            print("User doesn't exist or wrong details...")

答案 3 :(得分:0)

def Report1():
    found=False
    UserFind=input("Please enter a username")
    with open("Report.csv","r") as c:
        for row in c:
            details=row.split(",")
            if UserFind in details[0]:
                print(details[0])
                print(details[1])
                print(details[2])
                print("These are your user's details!")
                found= True
        if not found:
            print("User doesn't exist or wrong details...")

谢谢累积!