python - Issue trying to read spreadsheet line by line and write to excel (downsample) -


i trying write code downsample large excel file. needs copy first 4 lines exactly, on 5th line start taking every 40th line. have this

import os import string import shutil import datetime  folders = os.listdir('./') names = [s s in folders if "csv" in s] zips = [s s in folders if "zip" in s] folder in names:     filename = folder     shutil.move(folder, './archive')     open(filename) f:         counter = 0         line in f:             counter += 1             f_out = open('./downsampled/' + folder +  '.csv', 'w')             if counter < 5:                 f_out.write(line)             elif (counter+35) % 40 == 0:                 f_out.write(line)             f_out.close() 

it moves files archive folder, not create downsampled version, ideas on doing wrong here?

you're overwriting file on each iteration of previous file. move open(...) out of for loop:

with open(filename) f, open('./downsampled/' + folder +  '.csv', 'w') f_out:      i, line in enumerate(f):          if < 5:              f_out.write(line)          elif (i+35) % 40 == 0:              f_out.write(line) 

more so, enumerate can replace count logic.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -