2017-07-03 47 views
0

所以我有一個CSV,我試圖打印出頭6行,然後啓動代碼。我是超級新手。我知道如何跳過下一個(閱讀器)的前6行,但我需要打印前5行,因爲它是標題,然後在數據開始的第8行啓動腳本。所以我認爲這是讀者=之後的某種打印語句,然後可能是下一個(讀者)之後啓動代碼。只是我的理論。打印CSV中的前6行然後開始讀取

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 
+0

而不是圖像..你可以發佈一些示例輸出內容。所以我可以複製它並檢查? – void

+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 
+0

這個工程!只需要一些tweeking從CSV中擺脫所有醜陋的撇號和逗號。 –

+0

現在的問題是讓腳本開始在第8行上運行lol –

+0

我想你可以找出那一個;) –

0

那麼簡單跳過這些前六行然後使用一個簡單的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'], []] 
>>> 

所以基本上如果計數小於或等於六個I我只是追加那些沒人要的字符串stuff列表

stuff.append(line) 

如果線大於6我拆包他們爲你想要的。

hqline, part_no, part_class, appl_req, appl_count=line 

希望這對你很清楚。

Ofcourse您可以打印它們。我只是附加給你解釋這個概念。

相關問題