Last active
January 3, 2016 08:56
-
-
Save santiago-salas-v/e4884dca36884b33ca03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Concatenate csv files in folder ./DATA to folder ./folder_output, skipping line 1 | |
# of all except the first file | |
import os, csv | |
csv_folder = os.path.abspath('./DATA') | |
output_folder = 'folder_output' | |
files = [file for file in os.listdir(csv_folder) if os.path.splitext(file)[1].lower() == '.csv'] | |
if not os.path.isdir(output_folder): | |
os.mkdir(output_folder) | |
outfile_name = os.path.join(output_folder, 'output.csv') | |
while os.path.exists(outfile_name): | |
outfile_name = os.path.splitext(outfile_name)[0] + '-01' + '.csv' | |
outfile = open(outfile_name, 'w') | |
line_1 = True | |
first_file = True | |
for file in files: | |
csv_file_for_reading = open(os.path.join(csv_folder, file), 'r') | |
for line in csv_file_for_reading: | |
if line_1 and not first_file: | |
pass # skip line 1 | |
print 'skip line 1' | |
line_1 = False | |
else: | |
outfile.write(line) | |
line_1 = True | |
first_file = False | |
csv_file_for_reading.close() | |
print 'Done with ' + csv_file_for_reading.name | |
outfile.close() | |
print 'Done exporting to: ' + outfile.name | |
raw_input('Press ENTER ...') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment