How to read CSV files in Python
What is a CSV file?
Before diving into reading CSV files in Python, let’s understand exactly what a CSV file means.
CSV stands for comma-separated values, which means information is separated by commas. It is one of the most popular formats for importing and exporting processes in databases and spreadsheets. A CSV file is a text file, with a specific format that supports the saving of data in a table-structured format.
Within programming, these types of files are used very commonly (especially in data science and related fields).
How to read CSV files in Python?
There are different ways to read CSV files in Python.
We cover the most popular choices:
- Pandas is a Python library for data analysis and manipulation.
- CSV is a Python library for reading and writing tabular data in csv format.
Read CSV files with Pandas
The Pandas library has become one of the most popular Python libraries.
It is very efficient and well documented, and it provides concise coding methods.
Pandas is not built-in in Python, so it needs to be installed first.
Let’s dive into the coding part.
- First, we need to import Pandas (often abbreviated as “pd”).
import pandas as pd
- Second, we use the read_csv method from the Pandas library (store it in a variable).
csv_data = pd.read_csv("data.csv")
- Finally, display the data with a print statement.
print(csv_data)
Read CSV files with CSV library
The CSV library is another popular Python choice.
It is a built-in Python library, so it does not require prior installation.
Let’s see how does the code work.
- First, import the CSV library.
import csv
- Then, open the file with the “.open()” Python function (store in a variable).
csv_data = open("data.csv")
- Here, we implement the CSV reader method, used to read data (store in a variable).
reader = csv.reader(csv_data)
- Finally, print the data with a Python for loop.
for row in reader: print(row)
Full codes
The following are the full codes of both CSV reading methods.
Pandas
# Import library import pandas as pd # Open file csv_data = open("data.csv") # Display data print(csv_data)
CSV
# Import library import csv # Open file csv_data = open("data.csv") # Implement reader reader = csv.reader(csv_data) # Display data for row in reader: print(row)