Read a text file line by line and display each word separated by a '#' in Python
Aim:
To Read a text file line by line and display each word separated by a '#' in Python
Procedure:
- Open a file in read mode which contains a string.
- Use for loop to read each line from the text file.
- Again use for loop to read each word from the line splitted by ‘ ‘.
- 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()