Python通过islice循环读取20条记录

时间:2019-05-30 06:19:21

标签: python itertools

我正在读取TXT文件(1GB)。内容是很多URL记录。我想调用一个函数以返回20条记录。如何修改代码以允许他每次返回剩余的N条记录?这让我感到困惑。

from itertools import islice

def iter_list(start,stop):
    url = []
    with open("domain.txt") as file:
        for line in islice(file, start, stop):
            url.append(line)
        return url

def get_html(url):
    req = requests.get(url)
    print(req.status_code)
"""
     I want to extract 20 pieces to process after the cycle is completed. I don't know if this expression can be       understood. 
"""
url = iter_list(1, 20)
for i in url:
    get_html(i.strip())

1 个答案:

答案 0 :(得分:0)

from itertools import islice

def iter_list(start,stop):
    url = []
    tmp = []
    with open("domain.txt") as file:
        for line in islice(file, start, stop):
            url.append(line)
            if len(url) == 20:
               url.append(tmp)
               tmp = []
        return url


for i in iter_list(start,stop):
    print(i)