Read CSV File in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Reading CSV files is a common task that many of us encounter, whether it's for analyzing data, importing contacts, or processing logs. Python, with its simplicity and powerful libraries, makes this task almost effortless. In this guide on , we'll walk you through the basics of read csv file in python, ensuring you have all the tools you need to handle your data efficiently. No matter your experience level, you'll find these steps straightforward and easy to follow.

Reading a CSV File

Read CSV file using csv.reader()

Lets read csv file in python. Let's consider a csv Innovators_Of_Lang.csv shown below with csv.reader()

Code:

Output:

Explanation:

In the above example of understanding the concept of read csv file in python, we first start by opening the CSV file through the open() function in python and we know that it opens the Innovators_Of_Lang.csv. Once done, we start reading the file using the CSV.reader() that is returning the iterable reader object.

To iterate this reader object, we use the for-loop to print the contents of each row in the file. This way, we get the output as shown above, where we see each value is easily visible.

Reading CSV file using .readlines() function

When working with CSV files in Python, learning how to read CSV file in Python using the .readlines() function opens up another straightforward method to process our data line by line. Unlike specialized CSV reading functions that might automatically interpret the data structure for you, .readlines() gives you raw access to each line in the file, which you can then parse and utilize according to your needs. This technique is an essential skill for those looking to master read CSV file in Python, as it allows for greater control and customization in data handling.

Consider the same CSV file 'profession.csv' we used previously:

Code:

Output:

Explanation:

In the code above, we begin by reading all lines from the file using .readlines(), which returns a list of strings where each string is a line in the file. We then strip the newline characters from each line and split the string by commas to convert each line into a list of values. This approach provides a more manual but highly customizable way to process CSV data, especially useful when you need specific control over the parsing process. It's an excellent method for those looking to dive deeper into data handling with Python.

Read CSV file in python using Pandas

Yes, you heard it right! We can leverage the use of pandas library to read the csv file effortlessly. Let us use the below csv file to understand the use of pandas library.

employee.csv

Code:

Output:

Explanation: Here we made use of the pandas library to read the csv file. We can see that the csv file contains the date in the form of a string and we can leverage the opandas library to convert the string to date format with the help of the parse_dates optional parameter which basically defines the column to be treated as dates. We can also see that in the csv file we had different column names and in the output we have different we can implement the same by using the index_col optional parameter as shown in code.

Read CSV file using csv.DictReader

In order to read a CSV file as dictionary that is, key-value format we can make use of the objects of a csv.DictReader() class.

Let us consider below CSV file 'profession.csv' to understand the concept of csv.DictReader() in detail:

Code:

Output:

Explanation:

From above, we can see that the entries in the first row are dictionary keys along with the dictionary values being the entries in the other rows. In the code, we first start by creating dictionaries inside the for loop by explicitly using the dict() method that is, csv.DictReader() object. After the csv file was passing the csv.DictReader() object, we made the use of for loop to print each record of the csv file as a key-value(dictionary) pair.

The syntax of the csv.DictReader() class is:

Conclusion

  • Python simplifies working with CSV files, offering multiple methods to cater to different needs, from simple row-wise reading to complex data manipulation.
  • Depending on your specific requirement, whether it's reading data as dictionaries for easier key-value access with csv.DictReader or handling it row by row with .readlines(), Python provides a suitable approach.
  • For those dealing with large datasets or needing advanced data manipulation, the Pandas library stands out as an efficient, powerful tool for reading and analyzing CSV data.
  • Each method, whether it's csv.reader, .readlines(), or using Pandas, offers flexibility in how you process and interpret your CSV data, allowing for tailored data handling solutions.

Read More: