Created
July 21, 2021 04:09
-
-
Save okabe-yuya/2af76a619e2e5e72d8f6472736ce6def to your computer and use it in GitHub Desktop.
csvとtxtファイルを結合するためのそれ
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 pandas as pd | |
import os | |
import datetime | |
# ------------------------------------------------- | |
EXIST_DATA_DIR = "data" | |
FULL_PATH = os.getcwd() | |
DATA_PATH = os.path.join(FULL_PATH, EXIST_DATA_DIR) | |
ALLOWD_EXTENSION = [".csv", ".txt"] | |
# ------------------------------------------------- | |
if __name__ == '__main__': | |
print("*** Start merge csv files") | |
files = os.listdir(DATA_PATH) | |
files = list(filter(lambda f: f[-4:] in ALLOWD_EXTENSION, files)) | |
for file in files: | |
print(f"+++ {file}") | |
with_full_path = list(map(lambda f: os.path.join(DATA_PATH, f), files)) | |
pd_dfs = list(map(lambda f: pd.read_csv(f, encoding="cp932"), with_full_path)) | |
merged = pd.concat(pd_dfs) | |
nowtime_str = datetime.datetime.now().strftime('%Y%m%d-%H%M%S') | |
file_name = f"{nowtime_str}_merged.csv" | |
save_path = os.path.join(DATA_PATH, file_name) | |
merged.to_csv(save_path, header=True, index=False) | |
print(f"*** All finished🎉, created {save_path} file") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment