Using the Python Write to File Function
How can developers and clients benefit from data being in a file format when there are so many other options out there? Why not just use a simple HTML page with AJAX requests and tables to display the data? In this blog post, let me show you why using Python to construct files is productive way of of creating and filling them with data.
We will go over a few APIs to populate our files with data, and learn how to use the Requests HTTP library to fetch data. In this post, we’ll be using Python 3 and its toolkit.
Let’s get started!
Setting objectives
Our first step is to set some objectives for our files.
- First, we want some inspiration: to do that, we’re going to form a request to a famous quotes API using the Requests module in Python.
- Second, we want to make sure our investments in the stock market are looking solid. We’ll use IEXFinance Python wrapper around the Investors Exchange Developer API.
- Third, we also want to make sure our cryptocurrencies are staying green as well. We’ll fetch data with the Requests module from the Coinbase API to check on our digital assets.
- Last, we’ll take all this fetched data and write to files that correspond to each time we run the script and have one accumulative file. All of our files will be in CSV format.
I’m assuming that we’ll utilize the functionality of spreadsheet software to graph the data stored in the files. Let’s dive into how the Requests HTTP library works, and get our quotes!
Using the Requests module
The Requests HTTP Library is an elegant way of making HTTP requests without relying on manual labor of forming query-strings or to form-encode POST data.
Our primary focus will be making a GET
request to TheySaidSo or FavQs API to fetch our inspirational quote. To start, we need to install the Requests library in our project’s directory. In a Unix terminal, you can simply run pipenv install requests
if you have pipenv installed.
If you don’t have pipenv installed, use pip install pipenv
to install it:
Getting inspiration by fetching quotes
Now that you have the Requests library available to use, let’s form our first request. Since we’ll be tracking our favorite stocks and crypto coins, it may be a good idea to add hope to our file. We’ll then form an API Get request to theysaidso.com‘s quotes API.
The ‘quote of the day’ endpoint is straightforward to digest and will give us some inspiration when we need it most. Alternatively, you can also use the FavQs API. This quote API will allow for more requests if you plan to use the script more than 10 times per hour. Now it’s time to form that request and format the response!
Above, you’ll notice how we form the request and format the response. I provided an example for both APIs.
I’ll be referencing FavQs’s API since I’m not paying for TheySaidSo’s API. This way, I can always have a quote each time I run the script since I won’t hit their rate limit of 30 requests per 20 seconds.
First, we make the request, and when it succeeds, we receive a response which needs JSON decoding. After decoding the response, we are able to simply combine the dictionary key/value pairs into a formatted string. This string will be (‘quote’ – ‘author’).
Remember that we might come across commas in the quote we’re searching for. To account for this, we need to use the replace()
function to replace all commas with an HTML hex code value for commas, or any chosen value that’s still easily replaceable. Replacing it with the hex code value will ensure that future replacement is just as straightforward. Now we can store this string as a constant to use later when we write it to our file.
For our next example, we’re going to be looking at some different data: let’s fetch our favorite stock prices!
Back to Top