Reading a specific number of lines of a .csv in python
Question: I have a csv file that I am trying to read into python and then I want to
store the first two columns in a variable called name and gender. The current
code I am using is the following:

    
    
    import csv
    infile = open('blue.csv', 'r')
    csvfile = csv.reader(infile)
    
    name = []
    gender = []
    
    for row in csvfile:
        name.append(row[0])
        gender.append(row[1])
    

There are two problems I am encountering:

1) The csv file has headers so I don't want those included inside the
variables when I store the columns

2) I am missing the gender for the last row of the csv file and so I don't
want to include the last line of the csv file when I store it in a variable.

I am an `R` programmer and so to me, the way I would get around this is to
read in the file excluding the first row and last row but I am unsure of how
to do this in python, or better yet, if there is a better/smarter alternative.

If it helps, here is what a mock dataset would look like:

    
    
    Name, Gender
    Bob, Male
    Susan, Female
    Doug,
    


Answer: You wrote

> I am an R programmer and so to me, the way I would get around this is to
> read in the file excluding the first row and last row but I am unsure of how
> to do this in python

This can be done with
[`readlines`](https://docs.python.org/2/tutorial/inputoutput.html) and [list
slicing](https://docs.python.org/2.3/whatsnew/section-slices.html) like so:

    
    
    open('foo.csv').readlines()[1: -1]
    

Furthermore, note that
[`csv.reader`](https://docs.python.org/2/library/csv.html) takes both a file
object and a list:

> csvfile can be any object which supports the iterator protocol and returns a
> string each time its next() method is called — file objects and list objects
> are both suitable.

So you can just use:

    
    
    for l in csv.reader(open('foo.csv').readlines()[1: -1]):
        ...
    

