以CSV格式打印前6行,然后开始读取行

时间:2017-07-03 11:24:53

标签: python python-3.x csv

所以我有一个CSV,我试图打印前6行标题,然后启动代码。我是超级新手。我知道如何跳过下一行(读者)的前6行,但我需要前5行打印,因为它是标题,然后在第8行开始脚本,数据开始。所以我认为这是读者之后的某种打印声明=然后可能接下来(读者)启动代码。只是我的理论。

import csv

with open('GAT_US_PartReview_2017-06-23.csv', newline='') as f:
    reader = csv.reader(f)



    for row in reader:
        hqline, part_no, part_class, appl_req, appl_count, intr_req, 
        intr_count, oe_intr, has_oe_intr, has_attr_editor,
        part_IMG_req, has_part_img, has_part_img, has_mpcc, warr_req, 
        warr_txt, warr_pdf, msds_req, has_msds, upc_req, has_upc, 
        has_unspsc, attr_count, attrval_count, valid_part = row

CSV Import

Supplier Line:,,Gates Rubber - Denver (GAT),,,,,,,,,,,,,,,,,,,,,,
Summary:,,Parts HQ Abbr,,,,,,,,,,,,,,,,,,,,,,
ACCT No:,,40013586,,,,,,,,,,,,,,,,,,,,,,
RecCount:,,10221,,,,,,,,,,,,,,,,,,,,,,
Applicable Date:,,"June 14, 2017 (Wednesday)",,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,
HQ Line,Part No,Part Class,Appl Req,Appl Count ,Intr Req,Intr Count ,OE Intr Req,Has OE Intr,Has Attr Editor, Has Attr Values,Part IMG Req,Has Part IMG,Has MPCC,Warr Req,Has Warr TXT,Has Warr PDF,MSDS Req,Has MSDS,UPC Req,Has UPC,Has UNSPSC,Attr Count ,AttrVal Count ,Valid Part
GAT,'27210',S,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,13,YES
GAT,'27211',O,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,14,YES
GAT,'27212',S,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,13,YES
GAT,'27213',S,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,13,YES
GAT,'27220',S,NO,0,YES,2,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,35,20,YES
GAT,'27221',S,NO,0,YES,2,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,35,20,YES

2 个答案:

答案 0 :(得分:3)

为什么不尝试枚举?

import csv

with open('GAT_US_PartReview_2017-06-23.csv', newline='') as f:
    reader = csv.reader(f)

    for row_num, row in enumerate(reader):
        if row_num <= 5:
            print row  # Print out the header
        else:
            hqline, part_no, part_class, appl_req, appl_count, intr_req, 
            intr_count, oe_intr, has_oe_intr, has_attr_editor,
            part_IMG_req, has_part_img, has_part_img, has_mpcc, warr_req, 
            warr_txt, warr_pdf, msds_req, has_msds, upc_req, has_upc, 
            has_unspsc, attr_count, attrval_count, valid_part = row

答案 1 :(得分:1)

简单地跳过前六行,然后使用简单的if..else

示例csv文件:

Supplier,  GAT   
Summary, Parts   
ACCT no, 40404
RecCount, 10122
App Date, june 14,2017

HQ,Part No, Part class, Stuff, Other stuff
GAT,'27211',O,NO,0
GAT,'27212',O,YES,0
GAT,'99999',O,NOPE,0

<强>代码:

import csv

stuff = []

with open('test_file.csv', newline='') as f:
    freader = csv.reader(f)
    count=0
    for line in freader:
        count+=1
        if count>6:
            hqline, part_no, part_class, appl_req, appl_count=line
            print(hqline, part_no, part_class, appl_req, appl_count)
        else:
            stuff.append(line)


print("-----------------------------------")
print(stuff)

<强>输出:

HQ Part No  Part class  Stuff  Other stuff
GAT '27211' O NO 0
GAT '27212' O YES 0
GAT '99999' O NOPE 0
-----------------------------------
[['Supplier', '  GAT   '], ['Summary', ' Parts   '], ['ACCT no', ' 40404'], ['RecCount', ' 10122'], ['App Date', ' june 14', '2017'], []]
>>>  

所以基本上如果count 小于或等于 6,我只是将那些不需要的字符串附加到stuff列表。

stuff.append(line)

如果行数大于6,我就按照你的意愿打开它们。

hqline, part_no, part_class, appl_req, appl_count=line

希望你明白这一点。

当然你可以打印它们。我只是附上来向你解释这个概念。