Spaces:
Runtime error
Runtime error
# backend/csv_processor.py | |
import pandas as pd | |
def process_csv_for_dashboard(filepath: str) -> pd.DataFrame: | |
""" | |
Reads a CSV file and returns its content as a pandas DataFrame. | |
Args: | |
filepath (str): The path to the CSV file. | |
Returns: | |
pd.DataFrame: A DataFrame containing the CSV data. | |
Returns an empty DataFrame if the file cannot be read. | |
""" | |
try: | |
df = pd.read_csv(filepath) | |
# You might want to add more processing here, e.g., | |
# df.dropna(inplace=True) | |
# df.columns = [col.lower().replace(' ', '_') for col in df.columns] | |
return df | |
except FileNotFoundError: | |
print(f"Error: CSV file not found at {filepath}") | |
return pd.DataFrame() | |
except Exception as e: | |
print(f"Error processing CSV file: {e}") | |
return pd.DataFrame() | |
# Example Usage (for testing this module independently) | |
if __name__ == "__main__": | |
print("\n--- Testing CSV Processing ---") | |
# Create a dummy CSV file for testing | |
dummy_csv_content = """Name,Age,City,Review | |
John Doe,30,New York,This movie was amazing! | |
Jane Smith,24,Los Angeles,It was okay, nothing special. | |
Peter Jones,45,Chicago,Absolutely dreadful, what a waste of time. | |
Alice Brown,22,Houston,I'm so glad I spent my money on this. (sarcastic) | |
""" | |
with open("dummy_reviews.csv", "w") as f: | |
f.write(dummy_csv_content) | |
df = process_csv_for_dashboard("dummy_reviews.csv") | |
print("Dummy CSV DataFrame:") | |
print(df.head()) | |
# Clean up dummy file | |
import os | |
if os.path.exists("dummy_reviews.csv"): | |
os.remove("dummy_reviews.csv") | |