簡體   English   中英

如何從文本文件中將特定數量的行輸入到數組中,並重復該操作,直到使用完所有行

[英]How to input an specific amount of lines from a text file into an array and repeat the action untill all lines are used

我有一個89000行的文本文件。 我想要的是一次在函數中輸入100個名稱,但是要對整個文件執行此操作。

所以你可以說:

負載100線

用這些線做點什么

睡覺

重復

在此之前,我只使用for every line in file但不知道該怎么做。 for every 100 lines in file並實際使用這些行讀取信息。

使用itertools.islice ,您可以從可迭代對象中獲取選定的項目:

with open('/path/to/file') as f:
    while True:
        lines = list(itertools.islice(f, 100))  # similar to `f[0:100]`
        if not lines:
            break
        # process lines

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM