擅长:python、mysql、java
<p>作为补充,
如果您正在读取一个非常大的文件,并且不希望一次将所有内容读入内存,则可以考虑使用缓冲区,然后按结果返回每个单词:</p>
<pre><code>def read_words(inputfile):
with open(inputfile, 'r') as f:
while True:
buf = f.read(10240)
if not buf:
break
# make sure we end on a space (word boundary)
while not str.isspace(buf[-1]):
ch = f.read(1)
if not ch:
break
buf += ch
words = buf.split()
for word in words:
yield word
yield '' #handle the scene that the file is empty
if __name__ == "__main__":
for word in read_words('./very_large_file.txt'):
process(word)
</code></pre>