Selected Reading

Python response.iter_lines() Method



Response.iter_lines() method of the Python Requests module returns an iterator that yields lines of the response content. This method is particularly useful for processing line-by-line data such as logs or streaming APIs.

This method takes a chunk_size parameter to specify the number of bytes read into memory at once and a decode_unicode parameter to decode bytes to strings.

By default this method handles newline delimiters and yields complete lines by making it an efficient way to process large text responses incrementally.

Syntax

Following is the syntax and parameters of Response.iter_lines() method of the python Requests module −

requests.Response.iter_lines(chunk_size=512, decode_unicode=False, delimiter=None)

Parameters

Following are the parameters of the Response.iter_lines() method of Python Requests module −

  • chunk_size(optional): The number of bytes to read into memory at once. The default value is 512.
  • decode_unicode(optional): If this parameter is True then the response content will be decoded to Unicode using the response's character encoding. The default value is False.
  • decode_unicode(optional): This parameter specifies a custom delimiter for splitting the lines. The default value is None which means lines are split on newlines.

Return value

This method returns an iterator over the response content which yields lines of the response body.

Example 1

Following is the basic example of the Response.iter_lines() method of python Requests module −

import requests

url = 'https://www.google.com'
response = requests.get(url, stream=True)

for line in response.iter_lines():
    if line:
        print(line)

Output

b'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage"
--------------
--------------
entListener("click",K)});}).call(this);</script></body></html>'

Example 2

When processing data line by line and dealing with Unicode it's essential to correctly decode the bytes to a string. Heres an example that shows how to handle Unicode decoding while processing a large file line by line. Here is the example of it −

import requests

url = 'https://httpbin.org/stream/20'
response = requests.get(url, stream=True)

for line in response.iter_lines(decode_unicode=True):
    if line:
        print(line)

Output

{"url": "https://httpbin.org/stream/20", "args": {}, "headers": {"Host": "httpbin.org"
--------------
--------------
thon-requests/2.31.0", "Accept-Encoding": "gzip, deflate, br, zstd", "Accept": "*/*"}, "origin": "122.181.50.101", "id": 19}

Example 3

When processing large files or streams in Python by specifying a chunk size can be crucial for managing memory usage and ensuring efficient processing.

Heres an example of how to specify a chunk size while processing a large file line by line. Here is the example of it −

import requests

url = 'https://httpbin.org/stream/20'
response = requests.get(url, stream=True)

for line in response.iter_lines(decode_unicode=True):
    if line:
        print(line)

Output

b'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-IN">
--------------
--------------
entListener("click",K)});}).call(this);</script></body></html>'
python_modules.htm
Advertisements