Wednesday, 9 April 2025

CSV: An Example

  import csv


def accept():

    with open('players.csv','a',newline='') as f :

        c=csv.writer(f)

        n=int(input('enter no of lines : '))

        if f.tell()==0:

            c.writerow(['Name','games','goals'])

        for i in range(n):

            name=input("Enter name : ")

            games=int(input("Enter no of games played : "))

            goals= int(input("enter no of goals : "))

            l=[name,games,goals]

            c.writerow(l)

            

def disp():

    with open('players.csv','r') as f:

        c=csv.reader(f)

        tot=0

        header=next(c)

        print(header)

        for i in c :

            print(i)

            tot+=int(i[2])

        print('Total = ',tot)

accept()

disp()


No comments:

Post a Comment