Q. Write code to open file created in previous question and print it in following form:
Name : <name> Phone :< phone number>
Answer :-
file = open("contacts.csv", "r") data = file.readlines() for i in data : for j in range(len(i) +1 ) : if i[ j ].isdigit() : print("Name :",i[ : j-1]," Phone :",i[ j : ]) break
Output :-
Let contact.csv contains :-
Path | 1234567890 |
Portal | 5796423891 |
Walla | 1357926480 |
Express | 4668446545 |
So, Output :-
Name : Path Phone : 1234567890
Name : Portal Phone : 5796423891
Name : Walla Phone : 1357926480
Name : Express Phone : 4668446545
>>>
Name : Portal Phone : 5796423891
Name : Walla Phone : 1357926480
Name : Express Phone : 4668446545
>>>
In the bk, it's q 4, file "contacts.txt" is given and we have to print in the form given in the above question. But file was not previously created. How to write the program?
ReplyDeleteI think, we need to mention the drive where we are writing the program. For example first we create a file using notepad under any drive (say in the C or D or E drive of your computer) and then save it with a .txt or .csv extension. Then while writing the program we need to mention it like - file=open("E:\contacts.csv", "r")
DeleteThat is csv file.
DeleteIn the book, it's file "contacts.txt" , u have done.csv. Please correct it
ReplyDeleteyou did not enter the import module .first we have to import csv module
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )