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
What is the following code doing?
import csv File = open("contacts.csv", "a") Name = input("Please enter name: ") Phno = input("Please enter phone number: ") data = [Name, Phno] csvwriter = csv.writer(File) csvwriter.writerow(data) File.close()
Write code to open the file in the previous question and print it in the following form:
Name : <name> Phone: <phone number>
Consider the file "contacts.csv" created in above Q. and figure out what the following code is trying to do?
name = input("Enter name :") file = open("contacts.csv", "r") for line in file: if name in line: print(line)
Write a program to enter the following records in a binary file :
Item No — integer
Item_Name — string
Qty — integer
Price — floatNumber of records to be entered should be accepted from the user. Read the file to display the records in the following format:
Item No :
Item Name :
Quantity :
Price per item :
Amount : ( to be calculated as Price * Qty)