Jessy White
Jessy White

Reputation: 297

How to fix Index out of range in python

I have to extract two columns from a website. I know how to read the rows and print them but I'm having trouble extracting only the first and third column, every time I try to attach a variable to a row and try printing the variable it gives me an Error but if I use print(row[2]) it works but at the end it would say index out of range. I don't understand why? Here is what I did:

import urllib.request
import csv

with urllib.request.urlopen("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") as webpage:
    reader = csv.reader(webpage.read().decode('utf-8').splitlines())
    for row in reader:

        cor = row[0]
        cors = row[2]
    print(cors)

Upvotes: 0

Views: 197

Answers (2)

Learner
Learner

Reputation: 5292

My attempt

import urllib
import csv

webpage =urllib.urlopen("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")
reader = csv.reader(webpage.read().decode('utf-8').splitlines())
for row in reader:
    if row:
        cor = row[0]
        cors = row[2]
        print cor,cors

Upvotes: 3

Colin Schoen
Colin Schoen

Reputation: 2592

At the very end of the data set there is an empty row which is being stored as an empty list.

You can check for this by using the following condition:

import urllib.request
import csv

with urllib.request.urlopen("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") as webpage:
    reader = csv.reader(webpage.read().decode('utf-8').splitlines())
    for row in reader:
        if not row:
           continue   
        cor = row[0]
        cors = row[2]
    print(cors)

Upvotes: 3

Related Questions