2016-11-03 140 views
1

因此我試圖跳過.txt文件中的多行,然後使用csv閱讀器創建CSV文件。 18行需要跳過。這可以完成工作,但我確信有一個簡單的方法可以跳過18行而不是使用next()18次。在Python中跳過多行

import csv 
import os 

my_file_name = os.path.abspath('LensBank.txt') 
cleaned_file = "LensBankClean.csv" 
with open(my_file_name, 'r', newline='') as infile, open(cleaned_file, 'w',newline='') as outfile: 
    writer = csv.writer(outfile) 
    cr = csv.reader(infile, delimiter=',') 

    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    next(cr) 
    writer.writerow(next(cr)) 

    for line in (r[:20] for r in cr): 
    writer.writerow(line) 

這適用於我,但我怎麼會清理代碼到一個更簡單的版本。謝謝!

回答

3

使用:

for skip in range(18): 
    next(cr) 
+0

...它可以在'infile'上完成,然後創建'csv.reader'來保存幾個cpu週期。 – tdelaney

1
for i in range(18): 
    next(cr) 

使用for循環。或者你可以使用itertools.dropwhile

for line in (r[:20] for i, r in itertools.dropwhile(lambda x: x[1] < 18 , enumerate(cr))): 
1

這是奇怪的是,你用一個for循環之下,但沒有考慮它了同樣的問題。

你的代碼可以很容易的東西來替代這樣

for i in range(18): 
    next(cr) 
writer.writerow(next(cr)) 

這將調用next(CR)18次,事後致電writer.writerow

1

這個怎麼樣,

import csv 

# read a csv file into a list of lists 
with open(in_file, 'r') as f_in: 
    lists = [row for row in csv.reader(f_in, delimiter=',')] 

# write a list of lists to a csv file 
with open(out_file, 'w') as f_out: 
    writer = csv.writer(f_out) 
    writer.writerows(lists[18:]) # skip the first 18 lines 

正如@PatrickHaugh所述,上述解決方案對大文件無效。以下是大文件的解決方案。

with open(in_file,'r') as f_in, open(out_file,'w') as f_out: 
    # skip the first n lines 
    for _ in range(18): 
     next(f_in) 
    for line in f_in: 
     f_out.write(line) 
+1

這對於小文件來說是一個很好的解決方案,但對於非常大的文件,我覺得將整個內容讀入內存是不好的。如果不確定我的投入,我會非常不情願地這樣做。 –

+0

@PatrickHaugh,Thx。我同意。我剛剛更新了我的答案。 – SparkAndShine