Courseiva
StringshardMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

A data pipeline processes CSV lines that may contain quoted fields with commas inside double quotes. For example: 'John, "Doe, Jr.", 35'. The team needs to split such a line correctly. Which approach is best?

⚠ Common exam trap

Python Institute often tests the misconception that regex or manual string splitting is sufficient for CSV parsing, when in fact the `csv` module is the standard library solution that correctly handles all edge cases defined by the CSV format specification.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Use csv.reader([line]) to parse the line.

Python's `csv.reader` is specifically designed to handle CSV parsing according to RFC 4180, including quoted fields that contain commas, newlines, and embedded quotes. It automatically manages quote state and field boundaries, making it the most robust and Pythonic solution for this task.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Manually iterate over characters and track quote state.

    Why it's wrong here

    Manually tracking quote state while iterating characters is a hand-rolled state machine that must account for many subtle CSV rules: quotes only act as delimiters at the start of a field, a doubled pair of double quotes inside a quoted field is an escaped literal quote, and commas or newlines inside quoted fields are not separators. Getting all those transitions correct is easy to get wrong, especially for edge cases like leading/trailing whitespace or an unterminated quote. This approach is essentially reimplementing csv.reader from scratch, and the resulting code is harder to read, test, and maintain than simply delegating to the standard library's proven parser.

  • Use str.split(',') after removing all quotes.

    Why it's wrong here

    Calling str.split(',') after stripping all quote characters destroys the CSV structure: the quotes are not just noise, they mark which commas are data and which are delimiters. For a line like "a,""b",c, removing quotes leaves a,"b",c — actually if you remove all quotes from a,b,"c,d",e you get a,b,c,d,e, and split yields five fields instead of the intended three. The approach cannot distinguish a quoted field containing a comma from actual field separators, and it also silently corrupts fields that legitimately contain literal double-quote characters. There is also no reliable way to remove only the syntactic quotes without at least partially parsing the line, which is the problem the method is trying to avoid.

  • Use csv.reader([line]) to parse the line.

    Why this is correct

    csv.reader([line]) is the correct approach because csv.reader is a full CSV parser implementing the quoting rules (RFC 4180 and the dialect parameters such as quotechar, doublequote, escapechar, and delimiter). By passing [line] — a one-element list, not the raw string — you fulfill csv.reader's expectation of an iterable of lines, and it returns a single parsed row as a list of fields. It correctly handles commas embedded inside quoted fields, escaped double quotes ("" inside a quoted field), and quoted fields with surrounding whitespace, all without manual parsing or brittle regular expressions. This is exactly the kind of robust, tested behavior the Python standard library provides for CSV data.

  • Use re.split(r',(?=(?:[^"]*"[^"]*")*[^"]*$)', line)

    Why it's wrong here

    This regex attempts to split only on commas that are followed by an even number of quote characters before the end of the line, aiming to identify which commas are outside quoted fields. However, it fails as soon as quoted fields contain escaped double quotes (represented as two consecutive quotes), because the lookahead's quote-counting logic cannot distinguish a literal escaped quote from a syntactic closing quote. It also assumes commas outside quotes are always followed by an even number of remaining quotes, but a line with an unterminated quote or with quotes in unusual positions breaks the whole pattern. Additionally, the lookahead with a quantified character class can cause pathological backtracking on long lines, and the regex is nearly impossible to modify or debug compared to a purpose-built state machine like csv.reader.

About these practice questions

One of 169 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.