我将从特定行开始读取文件,并一次读取N行.到目前为止,我一次读取N行数:
from itertools import islice
n = 10
with open(fname, 'r') as f:
while True:
next_n_lines = list(islice(f, n))
for line in next_n_lines:
print line.rstrip()
if not next_n_lines:
break
Run Code Online (Sandbox Code Playgroud)
有关从特定行号开始阅读的任何帮助.
有一个简单的解决方案使用itertools.islice:
N = 100 # starting line number
n = 10 # size of a chunk
with open(fname) as f:
f = islice(f, N, None) # creates an iterator that starts after N lines
while True:
next_n_lines = list(islice(f, n))
for line in next_n_lines:
print line.rstrip()
if not next_n_lines:
break
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |