Computer Science
Write code to open the file in the previous question and print it in the following form:
Name : <name> Phone: <phone number>
Answer
Let the file "contacts.csv" include the following sample data:
Aniketh,9876546707
Sanjeeth,8976548689
Amrutha,6786778908
import csv
with open("con.csv", "r") as file:
csvreader = csv.reader(file)
for row in csvreader:
if row:
name = row[0]
phone = row[1]
print("Name:", name,"\t\t" "Phone:", phone)
Output
Name: Aniketh Phone: 7868478767
Name: Sanjeeth Phone: 6766474789
Name: Amrutha Phone: 8749374678
Related Questions
Write a statement in Python to perform the following operations:
(a) To open a text file "BOOK.TXT" in read and append mode
(b) To open a text file "BOOK.TXT" in write mode
(c) To open a text file "BOOK.TXT" in append mode
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()
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)