Lesson guide
What this Python exercise practices
Split CSV Row Into Fields is a intermediate practice lesson that focuses on loops, iteration, counters. It is designed to be solved in about 15 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Lists or strings
- Basic for loop syntax
Difficulty and time
- Level
- Intermediate
- Estimated time
- 15 minutes
Practice path
Summary
Parse a single CSV row into its constituent fields handling quoted fields and escaped quotes.
Problem statement
CSV rows contain fields separated by commas. Fields may be enclosed in double quotes to include commas or leading/trailing spaces. Inside a quoted field, a double quote is represented by two consecutive double quotes. Implement split_csv_row(row) that returns a list of field strings following these rules: - Fields are separated by commas unless inside double quotes. - If a field is quoted, remove the surrounding quotes and unescape doubled quotes ("" -> "). - Trim leading and trailing spaces for unquoted fields, but preserve spaces inside quoted fields. - Empty fields (between consecutive commas) are allowed and should be returned as empty strings. Do not use Python's csv module; implement parsing using loops and string operations.
Task
Write a function that splits a CSV row string into a list of field strings, handling quoted fields (with commas inside) and double-quote escaping.
Examples
Basic and quoted fields
Input
split_csv_row('"Smith, John",30,Engineer')
Output
['Smith, John', '30', 'Engineer']
Explanation
The first field is quoted and contains a comma; quotes are removed. Other fields are unquoted and trimmed.
Input format
A single argument: row (string) representing one CSV row.
Output format
A list of strings representing the parsed fields.
Constraints
Row length <= 1000 characters. Do not use the csv module. Assume fields use double quotes for quoting and double-quote escaping.
Samples
Sample input 0
split_csv_row('a,b,c')
Sample output 0
['a', 'b', 'c']
Explanation 0
Simple comma-separated fields.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.