Reading CSV files using Python 3 is what you will learn in this article. The file data contains comma separated values (csv). The comma is known as the delimiter, it may be another character such as a semicolon.
A CSV file is a table of values, separated by commas. To read a CSV file from Python, you need to import the csv module or the pandas module.
Python Programming Bootcamp: Go from zero to hero
csv stands for “comma-separated values”. they are a common file format for data exchange, storage, and editing. in fact, the .csv files you may open in a spreadsheet application (like excel) are just plain text files, with one very simple rule:
all of the fields in your records must be separated by commas.
For example, the following might be a small part of a sample spreadsheet in csv format:
Another example csv file:
The process will be:
Read CSV file
One of the first things you will need to do when creating a data-driven Python application is to read your data from a CSV file into a dataset. If you’re familiar with Excel, reading data from a CSV file is easy but if you’re new to CSV, let me show you how easy it is.
The most basic method to read a csv file is:
# load csv module
We import the csv module. This is a simple module to read/write csv files in python.
You can read every row in the file. Every row is returned as an array and can be accessed as such, to print the first cells we could simply write:
For the second cell, you would use:
It is better to have the data in arrays, because it’s easier to understand than those indices like ,, etc.
You can do that by adding the cells to a list during loading. The example below demonstrates this:
# load module
We creates two arrays: dates and scores. We use the append method to add the cells to the arrays.
If you want to use a different delimiter simply change the reader call:
If you have many csv files in an identical format, you can create a function for loading the data. That way you don’t have to write duplicate code.
For instance, if your csv files have the format (dates,scores) then you can write this code:
Given a csv filename, the function will read and parse the csv data. Its added to the arrays dates and scores and returned.
CSV Files can be read by the Pandas library in Python. The read_csv() function in Pandas is used to read CSV files. You must pass it a file-like object containing your data
Pandas is not part of the Python standard library, so you will need to install it with the pip package manager. Panda’s read_csv function can read multiple columns
import pandas as pd
Pandas uses its own data structure called a DataFrame (df), it is different than a Python list that you used with the csv module. Once a dataset has been read then many data manipulation functions become available.
To access a row you can use the index like this
Related course: Python Programming Bootcamp: Go from zero to hero