Simple way to Read a text file line by line and display each word separated by a # | Python Class 12 computer science 083 new

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



Method 1:

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()


'''Method 2:

#-------------OR------------------


filein = open("Mydoc.txt",'r')

for  line in filein:

     word= line .split()

     for w in word:

          print(w + '#',end ='')

     print()

filein.close()

U

Post a Comment

If you any doubts, Please let me know

Previous Post Next Post