Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Split CSV Row Into Fields

Parse a single CSV row into its constituent fields handling quoted fields and escaped quotes.

Python practice15 minLoops & IterationIntermediateLast updated March 15, 2026

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']

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 1

Input

split_csv_row('a,b,c')

Output

['a', 'b', 'c']

Simple comma-separated fields.