Python CSV阅读器跳过9个标题 [英] Python CSV reader to skip 9 headers

查看:146
本文介绍了Python CSV阅读器跳过9个标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)       

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

I Looking for a technique to skip the 9 headers rather than the range function. Any help would be appreciated. Below is a sample of the csv file

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


推荐答案

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

You can use itertools.islice() to skip a fixed number of lines:

from itertools import islice

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

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

跳过行,直到空行,这需要一个不同的方法。您必须检查每一行,并在遇到只有空单元格的单元格时停止阅读:

If you wanted to skip rows until the 'empty' row, that requires a different approach. You'd have to inspect each row and stop reading when you come across one that has only empty cells:

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 =''

Note that you want to leave newline handling to the csv.reader; when opening your file set newline='':

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

这篇关于Python CSV阅读器跳过9个标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆