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

>>> 


5 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. 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?

    ReplyDelete
    Replies
    1. I 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")

      Delete
  2. In the book, it's file "contacts.txt" , u have done.csv. Please correct it

    ReplyDelete
  3. you did not enter the import module .first we have to import csv module

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post