0

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

4

1 に答える 1

1

必要ありませんreadline(); fileそれ自体が行に対する反復子です。単語が空白で区切られていると仮定します。

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))
于 2013-02-28T05:07:57.743 に答える