Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bryaneaton/b6243a88718963cb0622edbad1e4033f to your computer and use it in GitHub Desktop.
Save bryaneaton/b6243a88718963cb0622edbad1e4033f to your computer and use it in GitHub Desktop.
merge csv to python xlsx
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# import required module
import os
import pandas as pd
# assign directory
directory = './match_files'
writer = pd.ExcelWriter('out.xlsx', engine='xlsxwriter')
all_files = []
# iterate over files in
# that directory
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
# checking if it is a file
if os.path.isfile(f):
all_files.append(f)
df_from_each_file = (pd.read_csv(f) for f in all_files)
for (idx, df) in enumerate(df_from_each_file):
df.to_excel(writer, sheet_name='data{0}.csv'.format(idx))
writer.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment