easy +10 pts

Parse CSV Row

Split a single CSV line into fields, respecting quotes, escaped quotes, and commas inside quoted fields.

Write a function `parse_csv_row(line: str) -> list` that takes a single line of CSV text and returns a list of fields (strings) as parsed according to the standard CSV rules: - Fields are separated by commas. - A field may be enclosed in double quotes. In that case, commas inside quotes do not separate fields. - A double quote inside a quoted field is escaped by doubling it (""). - A quoted field may contain newlines, but the input will never contain actual newline characters, so you can treat the entire input as one line. - Whitespace is preserved; no trimming is performed. - If the input is an empty string, return an empty list. - If a quoted field is not closed, treat the rest of the line as part of that field. You may not use the `csv` module. Your function must handle all edge cases correctly.

Constraints

Input length: 0 <= len(line) <= 1000. The input contains only printable ASCII characters (no newlines). The function must handle any valid combination of quotes and commas.

Example

>>> parse_csv_row('a,b,c')
['a', 'b', 'c']
>>> parse_csv_row('"a,b",c')
['a,b', 'c']
>>> parse_csv_row('"a""b",c')
['a"b', 'c']
>>> parse_csv_row('')
[]
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate character by character. Keep a flag to know if you are inside quotes.
When you see a double quote while inside quotes, check if the next character is also a double quote — if so, it's an escaped quote.
When a comma appears outside quotes, finalize the current field.
At the end, don't forget to append the last field (unless the line is empty).
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.