KnowledgeBoat Logo
|

Computer Science

Create a CSV file "Groceries" to store information of different items existing in a shop. The information is to be stored w.r.t. each item code, name, price, qty. Write a program to accept the data from user and store it permanently in CSV file.

Python Data Handling

4 Likes

Answer

import csv

with open("Groceries.csv", mode = 'w', newline = '') as file:
    writer = csv.writer(file)
    
    while True:
        item_code = int(input("Enter Item Code: "))        
        name = input("Enter Name of the Item: ")
        price = float(input("Enter Price: "))
        quantity = int(input("Enter Quantity: "))
        
        writer.writerow([item_code, name, price, quantity])
        choice = input("Wish to enter more records (Y/N)?: ")
        if choice.upper() == 'N':
            break

The file "Groceries.csv" includes following records:

101,Soaps,45.5,5
102,Atta,34.0,2
103,Maggie,70.0,1

Answered By

1 Like


Related Questions