Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create a csv file in Python

Getting Started

If you're learning programming, you'll soon find that one of the most common tasks you'll be doing is interacting with data. Data can come in many forms, but one of the most ubiquitous is the humble CSV (Comma Separated Values) file.

In this blog post, we'll learn how to create a CSV file using Python, a popular programming language known for its readability and simplicity. To make it as clear as possible, let's imagine a CSV file is like a spreadsheet. Each line is a row, and the values separated by commas in each line are like the cells in each row.

What is a CSV file?

Before we dive in, let's make sure we understand what a CSV file is. Imagine you have a table in a spreadsheet with some data. A CSV file is simply a way of taking that table and turning it into a text file. The name comes from the way the data is structured. Each row of the table becomes a line in the text file, and each cell in the row is separated by a comma.

For example, if you have a table like this:

Name     Age     Occupation
Alice    30      Doctor
Bob      40      Engineer

It would become a CSV like this:

Name,Age,Occupation
Alice,30,Doctor
Bob,40,Engineer

The csv Module in Python

Python has a built-in module for dealing with CSV files, conveniently named csv. A module in Python is like a toolbox. It contains some tools (in the form of functions and classes) that are designed for specific tasks. The csv module has tools to read and write CSV files.

To use it, we just need to import it at the start of our script:

import csv

Writing to a CSV File

Once we've imported the csv module, we can use its writer function to create a new CSV file.

Let's say we want to create a CSV file for the table we saw earlier. First, we need to open a new file using Python’s built-in open function. This is like creating a new blank spreadsheet.

Then, we create a writer object using the csv.writer function. This is like picking up a pen to start writing on our blank spreadsheet.

import csv

with open('people.csv', 'w', newline='') as file:
    writer = csv.writer(file)

In the open function, 'people.csv' is the name of the file we're creating, 'w' means we're opening the file in write mode, and newline='' is a technical detail to make sure our file works correctly on both Windows and Unix systems.

With our writer object, we can now start adding rows to our CSV file using the writerow method. This is like writing a new row in our spreadsheet.

import csv

with open('people.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(["Name", "Age", "Occupation"])
    writer.writerow(["Alice", "30", "Doctor"])
    writer.writerow(["Bob", "40", "Engineer"])

In this example, we're writing three rows to our CSV file. The first row is the header row, which describes what each column in our table represents. The next two rows are the data rows, which contain the actual data.

And that's it! We've created a CSV file using Python.

Conclusion

Creating a CSV file in Python is like writing a spreadsheet by hand, but instead of a pen, we're using Python's built-in csv module, and instead of paper, we're writing to a text file. This might seem like a small task, but it's a fundamental skill that you'll use over and over again as a programmer. Once you've mastered it, you'll be one step closer to becoming a data wrangler, able to massage and coax information out of even the most stubborn datasets. So, congratulate yourself on this achievement, and keep exploring the vast universe of programming. Remember, every line of code you write is a stepping stone to the next big discovery that awaits you.