Spaces:
Runtime error
Runtime error
File size: 1,646 Bytes
7fb74eb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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")
|