Prerequisites: Reading and Writing data in CSV
CSV files are parsed in python with the help of csv library. The csv library contains objects that are used to read, write and process data from and to CSV files. Sometimes, while working with large amounts of data, we want to omit a few rows or columns, so that minimum memory gets utilized. Let’s see how we can read a CSV file by skipping the headers.
Steps to read CSV columns into a list without headers:
- Import the csv module.
- Create a reader object (iterator) by passing file object in csv.reader() function.
- Call the next() function on this iterator object, which returns the first row of CSV.
- Store the headers in a separate variable.
- Iterate over remaining rows of the csv file and store them in another list.
- print this list to verify.
The below code reads the Data.csv file.
Below is the full implementation:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.