Downloading a compressed CSV file from a URL is a common task in data processing and analysis. In this article, we will demonstrate how to download a compressed CSV file from a URL using Python.
For this example, we will use the following URL to download a compressed CSV file: https://assets.upstox.com/market-quote/instruments/exchange/NSE.csv.gz
This URL provide access to the complete list of BOD contracts available for trading on the Indian stock exchanges in CSV format.
First, we will import the necessary libraries. We will need the urllib.request library to download the file, and the gzip library to decompress it.
Python Libraries :
import urllib.request
import gzip
Next, we will define the URL of the file we want to download and the file name we want to save it as.
url = 'https://assets.upstox.com/market-quote/instruments/exchange/NSE.csv.gz'
filename = 'NSE.csv'
Then, we will use the urllib.request library to download the file and save it to disk. We will open a connection to the URL using the urlopen() function, read the contents of the response, and save the contents to a file using the write() function.
response = urllib.request.urlopen(url)
with open(filename, 'wb') as f:
f.write(response.read())
Now we have downloaded the compressed file and saved it to disk. However, the file is still compressed, so we need to decompress it using the gzip library. We will use the GzipFile class to read the contents of the compressed file and write the uncompressed contents to a new file.
with gzip.open(filename, 'rb') as f_in:
with open('uncompressed.csv', 'wb') as f_out:
f_out.write(f_in.read())
Finally, we have the uncompressed CSV file that we can process using other tools, such as pandas or Excel.
In summary, to download a compressed CSV file from a URL using Python, we need to use the urllib.request library to download the file and the gzip library to decompress it. Once the file is downloaded and decompressed, we can process it using other tools as needed.
Discover the world of algo trading with Upstox’s soon-to-be-launched free API. Ready to explore? Open an account with us now using the link below and take the first step towards your trading journey: https://upstox.com/open-account/?f=1TFG

0 Comments