繁体   English   中英

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

[英]Python reads 20 records in a loop through islice

我正在读取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()) 

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM