![](/img/trans.png)
[英]Reading random lines from a file in Python that don't repeat untill 4 other lines have passed
[英]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.