Download CSV From URL in Python

Manav Narula Jan 30, 2023 Mar 29, 2022
  1. Use the pandas.read_csv() Function to Download a CSV File From a URL in Python
  2. Use the urllib and csv Modules to Download a CSV File From a URL in Python
  3. Use the requests and csv Modules to Download a CSV File From a URL in Python
  4. Conclusion
Download CSV From URL in Python

We can read data from the web in Python using different modules like requests, urllib, and more. A comma-separated text file (CSV) is a data file that can be read into a DataFrame using the Pandas package.

This tutorial demonstrates how to download a CSV file from a URL in Python.

Use the pandas.read_csv() Function to Download a CSV File From a URL in Python

The read_csv() function from the Pandas module can read CSV files from different sources and store the result in a Pandas DataFrame.

We can use this function to download CSV files from a URL in Python by providing the URL within the function directly.

Code:

import pandas as pd
df = pd.read_csv('https://sample.com/file.csv')

The above code will download the CSV file from the provided URL and store it in the DataFrame df.

Use the urllib and csv Modules to Download a CSV File From a URL in Python

The urllib module is used to work with and fetch URLs from different protocols in Python. We can use the urllib.urlopen() function to create a connection to a URL and read its contents.

This response can be processed using the csv module. The csv module works with CSV files in Python.

It can parse the response using the csv.reader() function. We can then display the parsed result at once or traverse through the content one row at a time.

Code:

import urllib
import csv
res = urllib.urlopen('https://sample.com/file.csv')
data = csv.reader(res)

Use the requests and csv Modules to Download a CSV File From a URL in Python

The requests is another module available in Python that can fetch data from URLs. It is a simple HTTP library with better error handling.

We can use the get() function from this module to fetch the response from the given URL of a CSV file. We use the iter_lines() function to iterate through the response content fetched by the get() function.

This content is then again parsed using the csv.reader() function to get the final data in the appropriate format.

Code:

import requests
import csv
res = requests.get('https://sample.com/file.csv')
t = res.iter_lines()
data = csv.reader(text, delimiter=',')

Conclusion

We discussed how to download a CSV file from a URL in Python. The pandas.read_csv() function is the most direct method as it automatically fetches and stores the file in a DataFrame.

The other methods require us to fetch the response and parse it using the csv module in Python to get the final result.

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Python HTTP