Comma separated value (CSV) files are a common file format used to store tabular data like spreadsheets or databases. Often you need to read data from a CSV file into a Python program for analysis and reporting. Python's built-in urllib module provides a simple way to download and parse CSV data.
In this article, we'll cover the key things you need to know to read CSV data from a URL using Python.
Downloading the CSV File
The first step is to download the raw CSV file contents from a URL. Here's an example using
import urllib.request
url = "https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv"
response = urllib.request.urlopen(url)
csv_data = response.read()
This opens the URL, gets the response, and reads the raw bytes into the
Parsing as CSV
Next we need to parse the CSV data. The built-in
import csv
rows = csv.reader(csv_data.splitlines())
for row in rows:
print(row)
Here we:
By default it expects commas as delimiters. You can customize the dialect and delimiters used.
Putting It Together
Here is the full code to download a CSV file from a URL and parse it with
import urllib.request
import csv
url = "https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv"
response = urllib.request.urlopen(url)
csv_data = response.read()
rows = csv.reader(csv_data.splitlines())
for row in rows:
print(row)
This provides the complete workflow to go from a URL to parsed CSV rows.
Dealing with Headers
Often CSV files have header rows describing each column. We can skip the headers easily:
next(rows) # skip header row
for row in rows:
print(row)
Use
Practical Considerations
Here are some things to keep in mind when using urllib to read CSV data:
Use Cases
Some examples of cases where using urllib to download and parse CSV data is helpful:
The direct URL access allows easily integrating remote CSV data into Python programs.
Next Steps
To take your CSV skills further:
I hope this overview gives you a solid starting point for using Python and urllib to access CSV file data!