2013-02-18 114 views
17

我有一个大的文本文件(〜7 GB)。我正在寻找是否存在读取大型文本文件的最快方法。我一直在阅读有关使用几种方法逐块阅读以加快这一过程。Python最快的方式来读取大文本文件(几GB)

在示例effbot为了处理96900行每秒文本的建议

# File: readline-example-3.py 

file = open("sample.txt") 

while 1: 
    lines = file.readlines(100000) 
    if not lines: 
     break 
    for line in lines: 
     pass # do something**strong text** 

。 其他authors建议使用islice()

from itertools import islice 

with open(...) as f: 
    while True: 
     next_n_lines = list(islice(f, n)) 
     if not next_n_lines: 
      break 
     # process next_n_lines 

list(islice(f, n))将返回下一n行文件f的列表。使用这个循环里面会给你的n线

+1

你为什么不检查你自己对你来说最快? – piokuc 2013-02-18 19:54:37

+0

在这里提出建议:http://stackoverflow.com/questions/14863224/efficient-reading-of-800-gb-xml-file-in-python-2-7 – BenDundee 2013-02-18 19:56:53

+0

@Nix我不想阅读一行一行,但块大块 – 2013-02-18 20:07:25

回答

9
with open(<FILE>) as FileObj: 
    for lines in FileObj: 
     print lines # or do some other thing with the line... 

将在时间存储器中读取一行,并在完成后关闭该文件块的文件...

+2

Morten逐行变得太慢了。 – 2013-02-18 20:05:14

+5

aay,读得太快...... – 2013-02-18 21:27:14

+0

看起来像FileObj循环的结果是单个字符,而不是行。 – 69444091 2017-06-02 01:13:03

相关问题