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)
I have provided the image of the CSV file and I have tried using line break to put the name, number, position, and date in vertical with a header but it's not working for some reason. I tried everything but it's not working someone please help.
this is the image of the CSV file
This is what the results should look like
If you want to print each field from the row on a separate line, just loop over them.
As an aside, probably refactor your function so it doesn't require interactive input.
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:"))
Probably a better design for most situations is to read the CSV file into memory at the beginning of your script, and then simply search through the corresponding data structure without needing to touch the disk again.