Created
November 21, 2019 17:48
-
-
Save taco-shellcode/bdaa3c75a23b898994956bb89b271bcb 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
import os | |
import csv | |
directory = "/path/to/csv_folder/" | |
headers = [] | |
merged_file_data = [] | |
for file_name in os.listdir(directory): | |
index = 0 | |
file_path = os.path.join(directory, file_name) | |
with open(file_path, "r") as csv_file: | |
csv_reader = csv.reader(csv_file) | |
for line in csv_reader: | |
index += 1 | |
if index == 1 and not headers: | |
headers.append(line) | |
elif index == 1: | |
continue | |
else: | |
merged_file_data.append(line) | |
with open("output.csv", "w") as outfile: | |
csv_writer = csv.writer(outfile) | |
csv_writer.writerows(headers + merged_file_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment