Fetch, modify, and update CSV file data
Filed under Accolite
Free read 3 of 3Split the raw string by lines and extract headers to find the column index of target_col.
The question
Given multi-line string data representing CSV contents, update numerical values in a target column by applying a floating-point multiplier, calculate total row sums, and return the modified content as formatted CSV string.
Constraints
- 1 <= number of rows <= 10,000
- CSV contains a header row with non-empty column names.
- Target column exists and contains valid numeric values.
Examples
- Input
- csv_data = "Name,Score\nAlice,10\nBob,20", target_col = "Score", multiplier = 1.5
- Output
- "Name,Score,Total\nAlice,15.0,15.0\nBob,30.0,30.0"
- Why
- The values in the 'Score' column are multiplied by 1.5 and written back with a new Total column.
Approach
Split the raw string by lines and extract headers to find the column index of target_col.
Iterate through data rows, parse values, multiply the target column value, and append total.
Join the modified header and row elements back into CSV format with newline separators.
Tips
- What they are testing: Verify behavior with trailing newlines, zero multipliers, and floating-point inputs.
- Common mistake: Failing to correctly handle header indexing or string-to-float type conversion errors.
Related Accolite questions
Collected from public candidate posts and coding forums. Updated Nov 2026.