Python csv跳过前两个空行

时间:2015-08-26 00:34:01

标签: python python-2.7 csv

在任何人将此标记为重复之前,我已尝试过来自isspace,startswith,itertools filterfunction,readlines()[2:]的所有内容。我有一个Python脚本,可以搜索数百个CSV文件,并在左边第八列中打印出匹配字符串(在本例中为唯一ID)的行。

import csv
import glob

csvfiles = glob.glob('20??-??-??.csv')
for filename in csvfiles:
    reader = csv.reader(open(csvfiles))
    for row in reader:
        col8 = str(row[8])
        if col8 == '36862210':
            print row

该代码适用于测试.csv文件。但是,我正在使用的真正.csv文件都有前两行空白。我收到此错误消息。

  

IndexError:列表索引超出范围

这是我的最新代码:

import csv
import glob

csvfiles = glob.glob('20??-??-??.csv')
for filename in csvfiles:
    reader = csv.reader(open(csvfiles))
    for row in reader:
        if not row:
            continue
        col8 = str(row[8])
        if col8 == '36862210':
            print row

2 个答案:

答案 0 :(得分:3)

尝试使用next跳过前两行:

import csv
import glob

csvfiles = glob.glob('20??-??-??.csv')
for filename in csvfiles:
    reader = csv.reader(open(filename))
    next(reader)
    next(reader)
    for row in reader:
        col8 = str(row[8])
        if col8 == '36862210':
            print row

答案 1 :(得分:0)

csv阅读器采用可迭代的,它可以是文件对象,但不一定是。

您可以创建一个生成器,从文件中删除所有空行,如下所示:

NSWindow

这个csvfile = open(filename) filtered_csv = (line for line in csvfile if not line.isspace()) 生成器会一直懒惰地从文件对象中拉出一行,如果该行完全是空格,则跳到下一行。

您应该能够编写如下代码:

filtered_csv

假设非空白行格式正确,即所有行都有第8个索引,则不应该得到for filename in csvfiles: csvfile = open(filename) filtered_csv = (line for line in csvfile if not line.isspace()) reader = csv.reader(filtered_csv) for row in reader: col8 = str(row[8]) if col8 == '36862210': print row

编辑:如果您仍然遇到IndexError,可能不是因为只包含空格的行。抓住异常并查看行:

IndexError

检查CSV读取器实际导致错误的输出。如果该行是不打印其内容的对象,请改为try: col8 = str(row[8]) if col8 == '36862210': print row except IndexError: pass