typeerror'_csv.reader'对象不可订阅

时间:2014-02-03 12:08:55

标签: python csv python-2.6

当我尝试在python中打开文件时,我收到错误,类型错误' _csv.reader'对象不可订阅。代码如下,有人可以帮助我

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

此处的错误在以下代码中,适用于阅读器[:1]中的行:

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

我需要跳过第一行,因为它有标题,这就是为什么我在做读者[:1]

1 个答案:

答案 0 :(得分:5)

您无法对reader个对象进行切片;您可以跳过第一行:

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