Created
March 7, 2022 16:22
-
-
Save bryaneaton/b6243a88718963cb0622edbad1e4033f to your computer and use it in GitHub Desktop.
merge csv to python xlsx
This file contains hidden or 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
#!/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