我试图用 headers 垂直打印 csv 数据,但它总是以水平线打印。有人可以看看我的代码吗

im trying to print the csv data in vertical with headers but instead it always prints in a horizontal line. can someone look my code plss

def players_stats():
    with open("playerstststsst.csv") as csvfile:
        csvreader = csv.reader(csvfile, delimiter=',',newline:'\n')
        player = (input("Enter the subject to use as filter:").lower().title())
        for row in csvreader:
            if player in str(row[0]):
                print(row)

我已经提供了 CSV 文件的图像和 我尝试使用换行符将名称、编号、位置和日期与 header 垂直放置,但由于某种原因它不起作用。我什么都试过了,但还是不行,有人请帮忙。

这是 CSV 文件的图片

enter image description here

结果应该是这样的

enter image description here

如果您想在单独的行上打印行中的每个字段,只需循环遍历它们即可。

顺便说一句,可能重构您的函数,使其不需要交互式输入。

def players_stats(player):
    player = player.lower().title()
    with open("playerstststsst.csv") as csvfile:
        csvreader = csv.reader(csvfile, delimiter=',',newline:'\n')
        for idx, row in enumerate(csvreader):
            if idx == 0:
                titles = row
            # row[0] is already a str, no need to convert
            elif player in row[0]:
                for key, value in zip(titles, row):
                    print("%s: %s" % (key, value))

# Let the caller ask the user for input if necessary
pläyers_stats(input("Enter the subject to use as filter:"))

对于大多数情况来说,更好的设计可能是在脚本开始时将 CSV 文件读入内存,然后简单地搜索相应的数据结构,而无需再次接触磁盘。