Q. Write a program to get item details (code, description and price) for multiple items from the user and create a csv file by writing all the item details in one go.
Answer :-
import csv fh = open("Items.csv", "w") iwriter = csv.writer (fh) ans = 'y' itemrec = [['Item_Name', 'Description"', 'Price']] print("Enter item details ") while ans == 'y' : iname = input("Enter Item code : ") desc = input("Enter description : ") price = float(input("Enter price: ")) itemrec.append([iname, desc, price]) ans = input ("Want to enter more items? (y/n)... ") else: iwriter.writerows(itemrec) print("Records written successfully.") fh.close()
Output :-
Enter item details
Enter Item code : 0012
Enter description : PEN
Enter price: 7.5
Want to enter more items? (y/n)... y
Enter Item code : 2514
Enter description : PENCIL
Enter price: 4
Want to enter more items? (y/n)... y
Enter Item code : 3678
Enter description : COPY
Enter price: 40
Want to enter more items? (y/n)... n
Records written successfully.
>>>
The file contain data like :-
Item_Name,"Description""",Price
0012,PEN,7.5
2514,PENCIL,4.0
3678,COPY,40.0
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )