The Wayback Machine - https://web.archive.org/web/20240205063201/https://www.geeksforgeeks.org/python-read-csv-column-into-list-without-header/
Open In App
Related Articles

Python – Read CSV Column into List without header

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:

  1. Import the csv module.
  2. Create a reader object (iterator) by passing file object in csv.reader() function.
  3. Call the next() function on this iterator object, which returns the first row of CSV.
  4. Store the headers in a separate variable.
  5. Iterate over remaining rows of the csv file and store them in another list.
  6. print this list to verify.

Example:

The below code reads the Data.csv file.

Below is the full implementation:

Python3




import csv
  
# reading data from a csv file 'Data.csv'
with open('Data.csv', newline='') as file:
    
    reader = csv.reader(file, delimiter = ' ')
      
    # store the headers in a separate variable,
    # move the reader object to point on the next row
    headings = next(reader)
      
    # output list to store all rows
    Output = []
    for row in reader:
        Output.append(row[:])
  
for row_num, rows in enumerate(Output):
    print('data in row number {} is {}'.format(row_num+1, rows))
  
print('headers were: ', headings)


Output:

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 25 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials