Created
July 29, 2019 19:14
-
-
Save estasney/dcc1e8eaa8ad6416bf8fe864f97d433f to your computer and use it in GitHub Desktop.
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
import pandas as pd | |
import os | |
SPREADSHEET_FOLDER = r"C:\Users" # Which folder are these files located in? | |
OUTPUT_FILEPATH = r"C:\Users" # Where should it go? | |
if not os.path.isdir(SPREADSHEET_FOLDER): | |
raise NotADirectoryError | |
if '.xls' not in OUTPUT_FILEPATH and '.csv' not in OUTPUT_FILEPATH: | |
raise Exception("Output path must have ext of .csv, .xls, or .xlsx") | |
frames = [] | |
for spreadsheet in os.listdir(SPREADSHEET_FOLDER): | |
name, file_ext = os.path.splitext(spreadsheet) | |
if file_ext == '.csv': | |
open_method = pd.read_csv | |
elif '.xls' in file_ext: | |
open_method = pd.read_excel | |
else: | |
print("File {} is not a spreadsheet, skipping".format(spreadsheet)) | |
continue | |
full_path = os.path.join(SPREADSHEET_FOLDER, spreadsheet) | |
df = open_method(full_path) | |
frames.append(df) | |
df = pd.concat(frames) | |
if '.xls' in OUTPUT_FILEPATH: | |
save_method = df.to_excel | |
else: | |
save_method = df.to_csv | |
save_method(OUTPUT_FILEPATH, index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment