Reputation: 21
I have an input file that contains several lines of text, some of which are blank lines separating indented paragraphs.
I want to print one line of output for each word in the input file using readline() . Could someone provide an example of code that does this ? Thanks
Upvotes: 0
Views: 1694
Reputation: 414315
You don't need readline()
; file
is an iterator over lines by itself. Assuming words are separated by whitespace:
with open(filename) as file:
for line in file:
words = line.split()
if words:
# each line in the output has exactly one word in it
print("\n".join(words))
Upvotes: 1