Read a text file line by line and display each word separated by a '#' in Python | CBSE 12

Read a text file line by line and display each word separated by a '#' in Python

Read a text file line by line and display each word separated by a '#'


Aim: 

 To Read a text file line by line and display each word separated by a '#' in Python

Procedure: 

  1. Open a file in read mode which contains a string.
  2. Use for loop to read each line from the text file.
  3. Again use for loop to read each word from the line splitted by ‘ ‘.
  4. Display each word seperated by '#' from each line in the text file.


Source Code: 

filein = open("mydoc.txt",'r')
line =" "
while line:
     line = filein.readline()
     #print(line)
     for w in line:
          if w == ' ':
               print('#',end = '')
          else:
               print(w,end = '')
filein.close()

#Prepared by Latest Tech Updates--
filein = open("Mydoc.txt",'r') for line in filein: word= line .split() for w in word: print(w + '#',end ='') print() filein.close()


Also Check Create a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message


Sample Source Code


Read a text file line by line and display each word separated by a '#'

Sample Output

Read a text file line by line and display each word separated by a '#'


If you any doubts, Please let me know

Previous Post Next Post

Contact Form