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