Create a CSV movies.csv to hold movies’ records like Movies ID, Movies name, and Rating (out of 5 stars) using the list.

from csv import writer

import csv

def create():

    f = open("e:\movies.csv","a")

    dt = writer(f)

    dt.writerow(['Movie_ID','Movie_Name','Movie_Rating'])

    while True:

        mv_id= input("Enter Movie ID:\t")

        mv_name = input("Enter Movie name:\t")

        mv_rating = input("Enter Movie Rating(out of 5 stars):\t")

        dt = writer(f)

        lst=[mv_id,mv_name,mv_rating]

        dt.writerow(lst)

        ch=input("Want to insert More records?(y or Y):")

        ch=ch.lower()

        if ch !='y':

            break

        print("Record has been added.")

    f.close()

def read_data():

    print("\nMovie's Data is:\n")

    f1 = open("e:\movies.csv","r")

    data = csv.DictReader(f1)

    for i in data:

        print(i)

    f1.close()

create()

read_data()

 

Output:

Enter Movie ID: 1
Enter Movie name: "Pathan"
Enter Movie Rating(out of 5 stars): 5
Want to insert More records?(y or Y):"y"
Record has been added.
Enter Movie ID: 2
Enter Movie name: "Kantra"
Enter Movie Rating(out of 5 stars): 4.5
Want to insert More records?(y or Y):"y"
Record has been added.
Enter Movie ID: 3
Enter Movie name: "RRR"
Enter Movie Rating(out of 5 stars): 5
Want to insert More records?(y or Y):"y"
Record has been added.
Enter Movie ID: 4
Enter Movie name: "Laal Singh Chaddha"
Enter Movie Rating(out of 5 stars): 3
Want to insert More records?(y or Y):

 

CSV File:

movie

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved