CSV Processor

CSV Processor

import os
import glob
import pandas as pd

# **********************************************************************************
#   Spotfire Data Function: CSV Converter
#   Description: Exports dataframe 'cm' as DataTable_cm.csv in the specified folder
#                and performs post-processing on CSV files
#   Input Parameters:
#       - ePath (String): Directory path for CSV export
#       - cm (DataFrame): Dataframe to export as DataTable_cm.csv
# **********************************************************************************

# **********************************************************************************
# Export 'cm' dataframe to CSV
# **********************************************************************************
# Export the cm dataframe as DataTable_cm.csv
cm_output_file = os.path.join(ePath, "DataTable_cm.csv")
cm.to_csv(cm_output_file, index=False)

# **********************************************************************************
# CSV post-processing: For each .csv file, read row 1 (header), and if a cell contains
# a "." character, split by "." and keep only the first part as the cell value
# **********************************************************************************
csv_files = glob.glob(os.path.join(ePath, "*.csv"))

for csv_file in csv_files:
    # Read all rows as data (no header row)
    df = pd.read_csv(csv_file, header=None)
   
    if len(df) > 0:
        for col in df.columns:
            value = df.at[0, col]
            if isinstance(value, str) and "." in value:
                df.at[0, col] = value.split(".")[0]
   
    # Overwrite the CSV file with the processed data (no header/index)
    df.to_csv(csv_file, index=False, header=False)