![](/img/trans.png)
[英]How to extract specific lines from a huge text file (>16GB) where each line starts with a string specified in another input file?
[英]Time efficient way to skip no of line from very large text file (16gb) using python
我有一個非常大的 16gb 文本文件。 我需要跳過任何一行。我想以省時的方式跳過這些行。 我正在使用 python 作為代碼。怎么做?
只需閱讀您要跳過的行數並將其丟棄:
with open(your_file) as f_in:
for i in range(number_of_lines_to_skip):
f_in.readline()
# your file is now at the line you want...
您還可以使用enumerate
來創建一個生成器,該生成器僅在您跳過您想要的行后才產生行:
with open(your_file) as f_in:
for line in (line for i, line in enumerate(f_in) if i>lines_to_skip):
# here only when you have skipped the first lines
第二個可能更快。
請注意,如果到達文件末尾,對文件 object 調用next
將引發StopIteration
。
go_to_line_number = some_line_number
with open(very_large_file) as fp:
for _ in range(go_to_line_number):
next(fp)
for line in fp:
# start your work from desired line number
pass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.