 python使用生成器读取大文件-500g
python使用生成器读取大文件-500g
  使用python生成器读取超大文件
def myreadlines(f,newline):
    buf = ""
    while True:
        while newline in buf:
            pos = buf.index(newline)
            yield buf[:pos]
            buf = buf[pos+len(newline):]
        chunk = f.read(4096)
        if not chunk:
            yield buf
            break
        buf += chunk
with open("input.txt") as f:
    for line in myreadlines(f,"|"):
        print(line)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
编辑  (opens new window)
  上次更新: 2024/01/26, 05:03:22
