Courseiva
Strings →easyMultiple Choice

PCAP Strings Practice Question

A data entry application reads a CSV file where each line contains fields separated by commas. However, some fields are enclosed in double quotes and contain commas inside, e.g., 'John,"Doe, Jr.",30'. The developer currently uses line.split(',') to parse each line, which incorrectly splits the quoted field. The developer wants a solution using only the Python standard library (no third-party packages). Which of the following is the best approach?

⚠ Common exam trap

The PCAP exam often tests the candidate's knowledge of the standard library's csv module as the idiomatic and correct way to parse CSV data, expecting candidates to recognize that manual string splitting or regex approaches are fragile and not recommended for production code.

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 the csv module's reader: import csv; next(csv.reader([line]))

The csv module's reader is specifically designed to handle CSV parsing according to RFC 4180, including quoted fields with embedded commas. By passing the line wrapped in a list to csv.reader, the developer gets a properly parsed list of fields without needing to manually handle quote escaping or comma splitting.

Answer analysis

Option-by-option breakdown

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

  • ✓

    Use the csv module's reader: import csv; next(csv.reader([line]))

    Why this is correct

    The csv module implements RFC 4180 quoting rules, so its reader correctly treats commas inside double-quoted fields as literal data rather than delimiters. Passing the single line in a list yields the parsed fields, and csv ships with the standard library.

  • ✗

    Use line.strip().split(',') and then manually merge fields that start with a quote

    Why it's wrong here

    Manually merging quote-prefixed fields still splits on every comma first, so quoted commas inside fields are already lost and cannot be reliably rejoined. This approach suits fixed, unquoted delimiters; the stem's embedded commas require quote-aware parsing, which the csv module provides.

  • ✗

    Iterate through each character, track whether inside quotes, and split on commas outside quotes

    Why it's wrong here

    Hand-rolling a character scanner reimplements CSV parsing and mishandles escaped double quotes, embedded newlines and other dialect rules, so it fails on real files. Such manual parsing is for bespoke, non-CSV formats; the standard library's csv module already handles quoting correctly and should be used.

  • ✗

    Write a regular expression that matches commas outside quotes

    Why it's wrong here

    A regular expression that matches commas outside quotes fails because CSV fields can contain escaped quotes (e.g., `""`), newlines within quoted fields, or inconsistent whitespace around commas, all of which break a simple regex pattern. It is tempting because regex excels at matching delimiters based on context, and it would be correct for a strictly formatted CSV where quoted fields never contain escaped quotes or line breaks.

About these practice questions

One of 421 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.