Python: Read CSV file with selected column

Posted: May 19, 2016 in Python
Tags:

Hi, here are the basic code to read from the CSV file with selected column only using Python.

#!/usr/bin/python

import csv

def read_file_csv (read_file):
    # i just want to read column 2, 3 and 4 from the csv file
    columns = [int(2, 3, 4)]
    with open(read_file, 'rb') as csvfile_read:
          reader = csv.reader(csvfile_read, delimiter=',')
          resultsCompareTo = []
          for row in reader:
                for i incolumns:
                            resultsCompareTo.append(row[i])
   csvfile_read.close()
   return resultsCompareTo;

data = read_file_csv('the_file.csv')
print data

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s