![](/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.