pt_csv
Module Path: pt_dev_utility.pt_csv
Description
CSV data processing utilities with pandas integration. This module provides functions for reading and processing CSV files with flexible column selection for Python 3.8+.
Functions
read_csv_as_dict_list(csv_file_path: str, custom_headers: Optional[List[str]] = None, selected_columns: Optional[List[str]] = None) -> List[Dict[str, Any]]
Description: Read CSV file and return selected columns as list of dictionaries.
This function provides flexible CSV reading with options for custom headers and column selection, returning data in a dictionary format suitable for further processing.
Parameters:
-
csv_file_path(str) - Path to the CSV file to be read -
custom_headers(Optional[List[str]]) - Custom column names to use instead of CSV headers. If None, uses the first row of the CSV as headers -
selected_columns(Optional[List[str]]) - List of column names to include in output. If None, includes all columns
Returns:
- List[Dict[str, Any]] - List of dictionaries where each dictionary represents a row with column names as keys
Raises:
- Exception - If CSV loading or processing fails
Example Usage:
import pt_dev_utility as pdu
# Example 1: Read all columns with default headers
data = pdu.read_csv_as_dict_list('employees.csv')
print(data)
# Output: [{'name': 'John', 'age': 30, 'city': 'NYC'},
# {'name': 'Jane', 'age': 25, 'city': 'LA'}]
# Example 2: Read with custom headers
data = pdu.read_csv_as_dict_list(
'data.csv',
custom_headers=['employee_name', 'employee_age', 'location']
)
print(data[0])
# Output: {'employee_name': 'John', 'employee_age': 30, 'location': 'NYC'}
# Example 3: Read specific columns only
data = pdu.read_csv_as_dict_list(
'employees.csv',
selected_columns=['name', 'age']
)
print(data)
# Output: [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}]
# Example 4: Combine custom headers with column selection
data = pdu.read_csv_as_dict_list(
'data.csv',
custom_headers=['full_name', 'years_old', 'city'],
selected_columns=['full_name', 'years_old']
)
print(data)
# Output: [{'full_name': 'John', 'years_old': 30},
# {'full_name': 'Jane', 'years_old': 25}]
# Example 5: Process the data
for row in data:
print(f"Name: {row['name']}, Age: {row['age']}")
Use Cases
- Data Analysis - Load CSV data for analysis and processing
- ETL Operations - Extract and transform CSV data before loading
- Configuration Files - Read configuration data from CSV files
- Report Generation - Process CSV reports with custom formatting
- Data Migration - Convert CSV data to different formats
Notes
- Uses pandas internally for robust CSV parsing
- Supports all pandas-compatible CSV formats
- Memory efficient for large files
- Handles missing values and data type inference automatically
- Custom headers override the first row of the CSV file