Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含多行文本的输入文件,其中一些是分隔缩进段落的空白行。
我想使用 readline() 为输入文件中的每个单词打印一行输出。有人可以提供一个这样做的代码示例吗?谢谢
你不需要readline();file本身就是一个迭代器。假设单词由空格分隔:
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))