Revenent
Revenent

Reputation: 5

Python: getting error list index out of range

File: https://drive.google.com/file/d/10z5jnxTj9olbsRIGDYiDv6GpLdsb1YAN/view?usp=sharing

This is my code:

x,y = [], []
import csv
with open('data2.txt','r') as X:
    reader = csv.reader(X,delimiter=' ')
    for row in reader:
        x.append(row[0])
        y.append(row[1])

Error:

IndexError                                Traceback (most recent call last)
<ipython-input-3-90b7f811ab4f> in <module>()
      5     for row in reader:
      6         x.append(row[0])
----> 7         y.append(row[1])

IndexError: list index out of range

Upvotes: 0

Views: 42

Answers (1)

Amiral ASTERO
Amiral ASTERO

Reputation: 365

If i you are doing what i think, try to use that line reader = csv.reader(X,delimiter=',') The delimiter is what will separate columns, in your file it's , not a space

EDIT

As said by tdelaney you can also just use reader = csv.reader(X). The comma is the default delimiter for CSV

Upvotes: 2

Related Questions