I have a large csv file of the client and shared via a url to download and I want to download it line by line or by bytes and I want to limit only for 10 entries.
I have the following code which will download the file, but i want here to download only the first 10 entries from the file, I don't want the full file.
#!/usr/bin/env python import requests from contextlib import closing import csv url = "https://example.com.au/catalog/food-catalog.csv" with closing(requests.get(url, stream=True)) as r: f = (line.decode('utf-8') for line in r.iter_lines()) reader = csv.reader(f, delimiter=',', quotechar='"') for row in reader: print(row)
I don't know much about
contextlib, how it will work with
with in Python.
Can anyone help me here, it would be really helpful, and thanks in advance.
The issue is not so much with
contextlib as with generators. When your
with block ends, the connection will be closed, fairly straightforwardly.
The part that actually does the download is
for row in reader:, since
reader is wrapped around
f, which is a lazy generator. Each iteration of the loop will actually read a line from the stream, possibly with some internal buffering by Python.
The key then is to stop the loop after 10 lines. There area couple of simple ways of doing that:
for count, row in enumerate(reader, start=1): print(row) if count == 10: break
from itertools import islice ... for row in islice(reader, 0, 10): print(row)