Nowadays, CSV files are used everywhere, from data analysis to web applications. If you are working with data in Python, it is important for you to know how to read a CSV file. A CSV (Comma Separated Values) is a plain text file to store information in a table form, where all the data will be differentiated by a comma.
In this blog, you will learn everything about what CSV files are, how you can use them with the help of Python’s built-in csv module, and other popular methods like pandas. So let’s get started!
Table of Contents:
What is a CSV file?
The CSV (Comma-Separated Values) file is basically a text file that is used to save data in a tabular format. In this file, each line is used to represent a row of data, and the values in that row are separated by commas. These values are known as fields. A CSV reader helps you to work with CSV files in Python. CSV files are also used to transfer data from one Excel spreadsheet or even database to another. This is because CSV files can be created and understood easily. A simple example of a CSV file is given below for your reference:
Name, Age, Country
Rahul, 30, India
Ashley, 25, England
Charlie, 35, USA
Reading a CSV File
There are basically 2 ways to read a CSV file in Python. You can use either the CSV module or the Pandas library to read a CSV file.
1. CSV Module: In Python, the CSV module makes it easy for you to read and write data in CSV files. It provides you with tools with which you can work with tables of data, like rows and columns, with the help of simple code. By using a CSV reader, you are able to open a CSV file and read the rows of the file, and utilize it within your Python code.
2. Pandas library: The Pandas library is a very important library in Python that helps you to work with data. It gives you easy and flexible options on how you can store, organize, and examine your data, particularly when using tables such as spreadsheets or CSV files.
It is a good choice for handling a large amount of data quickly and clearly.
Unlock Python: Power Up Your Programming Skills!
Dive into real-world coding projects and become confident in writing clean, efficient Python code.
Read a CSV file using the CSV Module
Now, we are going to discuss the different ways you can read a CSV file using the CSV module in Python:
1. Using csv.reader()
In this method, you have to open the CSV file using the open() function in ‘r’ mode. This means that you’re opening the file to read and giving yourself access to the file. After that, you have to use the reader() function from the CSV module, which converts the file into a CSV reader. This allows you to read each line of the file, one row at a time, so that you can read and use that data easily. A sample code is given below for your reference:
Code:
Output:
Explanation:
The above code is used to read a CSV file, which is named “people_data.csv” and displays the data inside that file. Each row in the data is printed one by one using a for loop. This helps you to see all the information in the file on the screen.
Get 100% Hike!
Master Most in Demand Skills Now!
2. Using csv.DictReader() class
This method works similarly to the regular way of reading a CSV file. At first, you need to open the function using the open() function. But, instead of using the normal CSV reader, you need to use the DictReader from the csv module. The difference is that the DictReader reads each row as a dictionary, where the column names in the first line of the file get converted to keys. This makes it easier to access data by the column names instead of using index numbers. A sample code is given below for your reference:
Code:
Output:
Explanation:
The above Python code is used to read the “people_data.csv” file. It uses the DictReader from the csv module. The DictReader treats the first row as the headers and converts each row into a dictionary. This makes it easier for you to access data by the names of columns.
Read a CSV File using Pandas
Now we are going to discuss how you can read a CSV file using Pandas in Python.
Using pandas.read_csv() method
It is very easy to read a CSV file in Python using the pandas library. You just need to use the read_csv() function, and all the data gets loaded from the file into a tabular format that is easy for you to work with. A sample code is given below for your reference:
Code:
Output:
Explanation:
The above code is used to read the CSV file using the Pandas library. It loads the data into a DataFrame, which is like a table. After that, it prints the entire table so that you can view the entire data in a clean and organized way. This method is popular because the pandas library makes it easy for you to work with and analyze data in a tabular format.
Handling Headers and Delimiters
CSV files don’t always follow the same format. Some files may not contain headers, while others may use semicolons ( ; ), pipes ( | ) instead of commas. These differences can cause errors if they are not handled properly. You can tell the CSV reader to treat the first row as data or set a custom delimiter for reading the file correctly. A sample code is given below for your reference:
Code:
Output:
Explanation:
This code opens a file named ‘people_data.csv‘ in read mode. It reads the data with the help of a CSV reader, but with a separation symbol like a pipe ( | ) rather than a comma. Then, it goes through each row in the file and prints the data line by line.
Best Practices for Reading CSV Files in Python
1. You should use with open() for managing file resources automatically.
2. You should always handle exceptions using try-except blocks.
3. Always use the pandas library if your CSV file consists of thousands of rows.
4. You should always print the sample data using head() in order to verify that they are loaded correctly.
5. You should avoid hardcoding the file paths. Instead, you can use relative paths or configuration files.
Start Coding in Python for Free: No Experience Needed!
Begin writing real Python code through interactive, beginner-friendly modules—completely free.
Conclusion
Reading CSV files in Python is a very simple and straightforward skill to learn, especially when you are working with data. Whether you are using the built-in CSV module or the pandas library, both these methods provide you with easy ways to understand and handle data that is present in tables. The CSV reader lets you read each row clearly, and with tools like DictReader or read_csv(), you can access data by column names and manage it better. Once you know how to use a CSV reader, working with real-world datasets becomes much easier and more efficient. If you are an aspiring Python developer, explore our blog and enroll in our Python Certification Course.
Reading CSV Files in Python – FAQs
Q1. Can I read a large CSV file without loading the whole file into memory?
Yes, you can read it line by line using the csv.reader() in a loop to save memory.
Q2. How do I read only specific columns from a CSV file?
By using the parameter usecols in pandas.read_csv(), you are allowed to select columns of choice.
Q3. Can a CSV file have spaces after commas?
Yes, and you can handle it by using skipinitialspace=True in the CSV reader.
Q4. What if the CSV file is empty or corrupted?
Always use error handling like try-except to catch and manage such issues safely.
Q5. Can I read a CSV file with different encoding formats?
Yes, just pass the encoding parameter like encoding=’utf-8′ when opening the file.