89DEVs

CSV in Python

On this page we explain:


What is a CSV file?

CSV stands for Comma Separated Values and contains structured data. It is machine-readable, because the values are, like the name suggests, separated by commas (or another clearly defined sign). Using this structure allows computers to know when one row ends and the next row begins.

Structure of a CSV file

The structure of a CSV file is similar to a spreadsheet. The first line usually contains some header data, which explains the content of the column. The next lines contain the actual data of the csv file. In this example the first column contains the country name and the second column contains the population. structure of a csv file It is possible to add even more columns and rows and sometimes csv files contain large amounts of data. The file size of a csv file can easily reach multiple GigaBytes (GB) of data. It is therefore important to check that the computer system has enough free resources like enough free memory (RAM) to handle such large amounts of data and to use the available resources efficient when working with CSV files.

What are CSV files used for?

CSV files are mostly used to export and store data from databases like SQL or spreadsheets like Microsoft Excel. For example a business could store orders from an online shop in a database and then export it to a CSV file and open it in Microsoft Excel or Google Sheets to make adjustments to the data.

Work with CSV files in Python

Python makes it easy to work with CSV files. There are two main ways to use CSV files in Python:
  • use the CSV module
  • use Pandas module
The difference is that the csv module is a built-in module, it is already available without any import statement. The Pandas module on the other hand needs to be imported first, but gives us more functionality when it comes to processing the data of our csv files. You can find our useful how-tos related to CSV files here:

                
        

Summary


Click to jump to section