VOOZH about

URL: https://thenewstack.io/python-for-automating-apis-create-a-trivia-quiz-csv-file/

⇱ Python for Automating APIs: Create a Trivia Quiz CSV File - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-07-08 07:45:09
Python for Automating APIs: Create a Trivia Quiz CSV File
sponsor-andela,sponsored-post-contributed,
CI/CD / Data / Python

Python for Automating APIs: Create a Trivia Quiz CSV File

With Python, it is possible to automate operations and work with APIs to get information as structured data like JSON, XML and CSV files. Here's how.
Jul 8th, 2024 7:45am by Teri Eyenike
👁 Featued image for: Python for Automating APIs: Create a Trivia Quiz CSV File
Image from MisterStock on Shutterstock
Andela sponsored this post.

Technology is changing the scope of work, and automation is one way to do tasks faster with less time and effort.

With advanced tools and programming languages like Python, it is possible to learn to automate things like file operations, spreadsheets, email, file and directory management, and even work with different APIs to get desired information as structured data like JSON, XML, CSV (comma-separated value) and so on.

Developers are always looking for ways to improve and make the software development life cycle more efficient. Python, with its simple syntax and ability to process and handle complex tasks, is a handy tool for this purpose.

The Role of Automation

Automation involves creating scripts and applications that automatically perform repetitive tasks without human intervention or assistance.

If you work with large datasets or do manual tasks to compute data, automation is valuable for:

  • Software development
  • Marketing professionals
  • Data science
  • Business analysts and financial analysts

The following guide will help you understand how to interact with APIs to take automation to the next level. It will also help you integrate an API into a program to fetch its data sources and generate a list of CSV trivia questions on demand, along with the answers, using the Trivia API.

Setup and Installation

To ensure your scripts can handle connecting and retrieving information from websites and APIs (server), the Python requests module is required:

pip install requests

The requests module is an HTTP library for exchanging information over the internet.

The other modules, `csv` and `html` come pre-installed as part of the Python standard library.

  • csv: This module creates a CSV file.
  • html: This helps to decode or unescape html entities or ASCII characters into their original characters in plain text.

Building the Trivia QA Project

First, let’s set up the program by creating a file called main.py in any code editor you choose. This program will run in your terminal’s CLI.

Make sure the file is contained within a folder.

These three libraries will do all the work for the program, including sending the HTTP requests and writing the response to a CSV file.

Next, let’s prompt the user to do the following with the `input` function:

This allows the user to enter the number of trivia questions they’d like to see

Also, prompt the user to specify the difficulty of the questions:

Define the API endpoint

Use the Open Trivia Database as shown in the URL variable:

Set Up Request Parameters

Request parameters offer a structured way to send additional or optional information through key-value pairs that append to an API URL after the question mark. Therefore, the request parameter allows both the client and the server to have a more specific interaction, enabling the server to return data that is aligned more closely with what the client or user is looking for.

Define and set the following request parameters to the `request_params` variable:

The value for each key in the `request_params` represents information like amount, difficulty and category from the Trivia API. The category value set to “18” relates to the “Science: Computers” question category.

Requesting JSON and Extracting the Data With Python

To fetch data from a web server, the `GET` request is paired to the Trivia API using the `requests` library:

The `response` variable accepts the API URL, `headers` that format the server’s data as JSON and the parameters dictionary.

To extract the trivia data, parse the response as JSON using the `.json` method of the response object:

Initialize a List

Initializing a list is important at this stage to hold the question-and-answer pairs later in the saved CSV.

The header row starts with Question and Answer.

Looping Through the Trivia Data

Handling each item in the data is achieved using the `for loop` as shown:

This arrangement helps to unescape the HTML entities in the question and answer using the `html.unescape` method. For example, `&lt; &amp;` will be converted to `<, &` where such occurrence appears.

Also, if the question type is a Boolean, prepend `“True or False? “` to the question.

Now, append each question and its correct answer to the list to create a new row using the `qna` variable:

Creating the CSV

The final step is to write the question-and-answer pairs to a CSV file named “tech_trivia.csv” with the code:

This code block opens the file in write mode with newline set to an empty string. Thereafter, use the writer method of the CSV library to create a CSV writer object and write the rows to the file using the `writerows` method.

When the file is executed with the command `python main.py` in the terminal, the message “File created” appears, signifying that the file was created!

Conclusion

Automation has made simple tasks autonomous. For example, using Python code and API, you can generate a file populated with trivia questions and answers.

With this knowledge, you can do a lot more by making boring tasks enjoyable, and everyone benefits without the need for manual data computation.

As technology evolves and businesses become more reliant on software, Python development has become one of the most in-demand skills in the job market. However, finding the right Python developer for your company can be daunting. Read this guide to navigate the hiring process for a Python developer, including defining your project needs and evaluating and hiring candidates.

Andela provides the world’s largest private marketplace for global remote tech talent driven by an AI-powered platform to manage the complete contract hiring lifecycle. Andela helps companies scale teams & deliver projects faster via specialized areas: App Engineering, AI, Cloud, Data & Analytics.
Learn More
The latest from Andela
Hear more from our sponsor
TRENDING STORIES
Teri Eyenike is a software engineer and a member of the Andela talent network, a private global marketplace for digital talent. With more than five years of experience focused on creating usable web interfaces and other applications using modern technologies,...
Read more from Teri Eyenike
Andela sponsored this post.
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.