Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeerror '_csv.reader' object is not subscriptable [duplicate]

When I try to open a file in python I get the error, typeerror '_csv.reader' object is not subscriptable. The code is as below, can someone kindly help me please

with open(file) as f:  
    reader = csv.reader(f, delimiter='\t')  
    for line in reader:  
        oldseq, city, state, newseq = line  

The error is here, in the following code, for line in reader[:1]:

with open(newfile) as f:  
    reader = csv.reader(f, delimiter='\t')  
    for line in reader[:1]:  
        oldseq, city, state, newseq = line  

I need to just skip the first line as it has headers, thats why I was doing reader[:1]

like image 633
user1345260 Avatar asked Feb 03 '14 12:02

user1345260


1 Answers

You cannot slice a reader object; you can skip the first row with:

with open(newfile) as f:  
    reader = csv.reader(f, delimiter='\t')
    next(reader, None)  # skip header
    for line in reader:  
        oldseq, city, state, newseq = line  
like image 85
Martijn Pieters Avatar answered Sep 21 '22 11:09

Martijn Pieters