Merge multiple csv files into one using Python

I wanted to merge csv files with Facebook leads, the files use encoding utf-16 and tab-delimited separator.
You can change the encoding and separator to any other
df = pd.read_csv(file_path, encoding='utf-16', sep="\t")
In the folder_path specify the folder where your csv files are located.

import pandas as pd
import os

# Set the directory containing the CSV files
folder_path = r"C:/Users/path/to/folder"

# Get a list of all the CSV files in the directory
# Create an empty list to store the dataframes
dfs = []

# Loop through all CSV files in the folder and append them to the list
for file_name in os.listdir(folder_path):
if file_name.endswith('.csv'):
file_path = os.path.join(folder_path, file_name)
# csv files I'm merging have utf-16 encoding and using tab-delimated separator
df = pd.read_csv(file_path, encoding='utf-16', sep="\t")
dfs.append(df)

# Concatenate all dataframes into a single dataframe
merged_df = pd.concat(dfs, ignore_index=True)

# Save the merged dataframe to a new CSV file with encoding UTF-16
merged_df.to_csv('merged_file.csv', encoding='utf-16', index=False)

This script uses the os module to loop through all files in the specified folder, and the pandas library to read the CSV files and concatenate them into a single dataframe. The merged dataframe is then saved to a new CSV file named "merged_file.csv" in the same folder with encoding UTF-16. Note that you need to make sure that all CSV files in the folder have the same structure (i.e., the same columns) to avoid issues when concatenating them.

Published