Python CSV阅读器跳过9个标题

时间:2015-02-19 09:42:26

标签: python csv python-3.x

import os
import csv

def get_file_path(filename):
    currentdirpath = os.getcwd()
    file_path = os.path.join(os.getcwd(), filename)
    print(file_path)
    return(file_path)

path = get_file_path('Invoice-Item.csv')

def read_csv(filepath):
    with open(filepath, 'r') as csvfile:
        reader = csv.reader(csvfile)
        for i in range(0, 9):            
            next(reader, None)        
        for row in reader:
            print(row[0])                   

read_csv(path)       

我正在寻找一种技术来跳过9个标题而不是范围函数。任何帮助,将不胜感激。以下是csv文件的示例

Summary Journal Entry,JE-00000060
Journal Entry Date,28/02/2015
Accounting Period,Feb-15
Accounting Period Start,1/02/2015
Accounting Period End,28/02/2015
Included Transaction Types,Invoice Item
Included Time Period,01/02/2015-09/02/2015
Journal Run,JR-00000046
Segments,
,
Customer Account Number,Transaction Amount
210274174,545.45
210274174,909.09
210274174,909.09
210274174,909.09
210274174,909.09

3 个答案:

答案 0 :(得分:2)

您可以使用itertools.islice()跳过固定数量的行:

from itertools import islice

next(islice(reader, 9, 9), None)        
for row in reader:
    print(row[0])                   

指示islice()对象跳过9行,然后立即停止而不产生进一步的结果。它本身就是一个迭代器,所以你需要在它上面调用next()

如果要跳过行直到“空”行,则需要采用不同的方法。当您遇到只有空单元格的行时,您必须检查每一行并停止阅读:

for row in reader:
    if not any(row):  # only empty cells or no cells at all
        break

for row in reader:
    print(row[0])                   

后一种方法的演示:

>>> import csv
>>> import io
>>> sample = '''\
... Summary Journal Entry,JE-00000060
... Journal Entry Date,28/02/2015
... Accounting Period,Feb-15
... Accounting Period Start,1/02/2015
... Accounting Period End,28/02/2015
... Included Transaction Types,Invoice Item
... Included Time Period,01/02/2015-09/02/2015
... Journal Run,JR-00000046
... Segments,
... ,
... Customer Account Number,Transaction Amount
... 210274174,545.45
... 210274174,909.09
... 210274174,909.09
... 210274174,909.09
... 210274174,909.09
... '''
>>> with io.StringIO(sample) as csvfile:
...     reader = csv.reader(csvfile)
...     for row in reader:
...         if not [c for c in row if c]:
...             break
...     for row in reader:
...         print(row[0])                   
... 
Customer Account Number
210274174
210274174
210274174
210274174
210274174

请注意,您希望将换行处理留给csv.reader;打开文件集时newline=''

with open(filepath, 'r', newline='') as csvfile:

答案 1 :(得分:1)

如果您正在使用numpy,请查看genfromtxt(http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html)中的skip_header参数

import numpy as np     
r = np.genfromtxt(filepath, skip_header=9, names = ['account','amount'] , delimiter = ',')
print(r.account[0],r.amount[0])

答案 2 :(得分:1)

如果您考虑使用pandas,read_csv会使阅读文件变得非常简单:

import pandas as pd

data = pd.read_csv(filename, skiprows=9)