Primus_Pilus
Primus_Pilus

Reputation: 21

using readline in python to print out own line for each word of an input file that is being read

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

Answers (1)

jfs
jfs

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

Related Questions