Skip to main content

Hi Front Community! A lot of times you might need to request a full (or multiple) inbox export from Front Support. These exports follow the following file structure:

Inboxes → Inbox → Conversation → Messages.csv

The export format provided can make it challenging to get a clear overview of all messages within an inbox. This is a common issue for teams that frequently conduct inbox audits to review messages for analytics or other purposes.

To address this, I’ve developed a Python script that consolidates all messages.csv files from Front Support inbox exports into a single consolidated Messages CSV file for each inbox:

import os
import csv

'''
This is for a multiple inbox export.

Please note if the exports are quite large you should consider chunking output CSVs.
This is because applications like Excel have a row limit on CSVs that is just over 1 million.

Expected file structure is:
Inboxes -> Inbox folder -> Conversation folder -> Messages.csv
'''

# Path to the Inboxes folder
parent_folder_path = r'file\path\here'

# Dictionary to store rows for each inbox folder
inbox_rows = {}

csv.field_size_limit(10**6) # Increases the maximum allowable field size for CSV parsing due to the size of fields in the Text column of messages.csv

try:
# Get list of inbox folders
inbox_folders = os.listdir(parent_folder_path)
total_inboxes = len(inbox_folders)
print(f"Processing {total_inboxes} inbox folders...")

# Iterate through each inbox folder
for inbox_folder in inbox_folders:
inbox_folder_path = os.path.join(parent_folder_path, inbox_folder)

if os.path.isdir(inbox_folder_path):
# Initialize rows for the current inbox folder
inbox_rowsbinbox_folder] = l]

# Get list of conversation folders
conversation_folders = os.listdir(inbox_folder_path)
total_conversations = len(conversation_folders)
print(f"Processing {total_conversations} conversation folders in {inbox_folder}...")

# Iterate through each conversation folder
for conversation_folder in conversation_folders:
conversation_folder_path = os.path.join(inbox_folder_path, conversation_folder)

if os.path.isdir(conversation_folder_path):
messages_csv_path = os.path.join(conversation_folder_path, 'messages.csv')

if os.path.exists(messages_csv_path):
try:
with open(messages_csv_path, mode='r', newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
headers = next(reader) # Read and skip the header row

# Add new column headers
headers.append('Conversation ID')
headers.append('Inbox Folder')

for row in reader:
# Append the current folder names as new columns
row.append(conversation_folder)
row.append(inbox_folder)
inbox_rowsbinbox_folder].append(row)

except Exception as e:
print(f"Error reading file {messages_csv_path}: {e}")

# Write rows for the current inbox folder to a CSV file
if inbox_rowsbinbox_folder]:
output_csv = f'{inbox_folder}.csv'

file_exists = os.path.exists(output_csv)

if file_exists:
print(f"Output file {output_csv} already exists. Appending to the existing file.")

try:
with open(output_csv, mode='a' if file_exists else 'w', newline='', encoding='utf-8') as outfile:
writer = csv.writer(outfile)

if not file_exists:
writer.writerow(headers) # Write headers only if the file doesn't exist

writer.writerows(inbox_rowsbinbox_folder])

print(f"Written CSV for inbox folder {inbox_folder} to {output_csv}")

except Exception as e:
print(f"Error writing to file {output_csv}: {e}")

except Exception as e:
print(f"Error processing files: {e}")

 

Nice work, Andy!


After consolidating individual messages.csv files for each inbox using the code above, your team might want to run an additional step: merging all these inbox-specific Messages CSV files into a single, comprehensive messages.csv file.

This code will accomplish that:

import os
import pandas as pd

def merge_csvs_in_directory(directory_path, output_filename):
# List to hold all dataframes
all_dataframes = []

# Iterate over all files in the directory
for filename in os.listdir(directory_path):
# Check if the file is a CSV
if filename.endswith(".csv"):
# Construct the full file path
file_path = os.path.join(directory_path, filename)
# Read the CSV file into a DataFrame
df = pd.read_csv(file_path)
# Append the DataFrame to the list
all_dataframes.append(df)

# Concatenate all dataframes in the list
merged_df = pd.concat(all_dataframes, ignore_index=True)

# Save the merged dataframe to a new CSV file
merged_df.to_csv(output_filename, index=False)
print(f"Merged CSV saved as {output_filename}")

directory_path = r'File/Path/Here' # Where the Inbox messages.csv files live
output_filename = 'merged_output.csv'

# Call the function to merge CSVs
merge_csvs_in_directory(directory_path, output_filename)

 

**Keep in mind that CSV viewers, like Excel, often have limitations on the number of rows they can display. Be sure to monitor the total number of rows in the merged file to avoid any issues with viewing or processing the data if using something like Excel.**


Reply