PCAP Strings Practice Question
A junior developer is parsing a log file where each line has comma-separated fields. However, some fields are enclosed in double quotes and contain commas inside, e.g., '2023-08-15 14:30:00,WARNING,"Disk space low, please clean up".'. They are required to parse these lines using only built-in string methods (no modules like csv or re). Which approach is the most reliable and efficient?
⚠ Common exam trap
Many candidates choose Option A (removing quotes) or Option D (split and merge) because they seem simpler, but they fail to realize that those methods break when commas appear inside quoted fields, which is the exact scenario the question describes.
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
✓
Write a custom parser that iterates over characters, toggles a quote flag, and splits on commas when outside quotes
A custom parser that toggles a quote flag while iterating over characters can reliably distinguish commas inside double quotes from field-separating commas. This approach handles embedded commas and escaped quotes without relying on external modules, making it both efficient and robust for the given constraints.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Remove all double quotes from the line before splitting
Why it's wrong here
Stripping quotes destroys the field boundaries that the quotes exist to protect, so the embedded comma still splits the message into two fields and the text is corrupted. It is tempting because it is a one-line fix for simple quoted values, and would work only where no quoted field contains a delimiter.
- ✓
Write a custom parser that iterates over characters, toggles a quote flag, and splits on commas when outside quotes
Why this is correct
A character-by-character parser tracking a quote flag splits only on commas outside quoted sections, correctly handling embedded commas within fields. Built-in string methods suffice, and it avoids the incorrect splits a naive comma split would produce, meeting the no-modules constraint.
- ✗
Use a regular expression that matches commas outside double quotes
Why it's wrong here
Regular expressions are unavailable here because the question forbids the re module, and hand-rolled quote tracking via built-in string methods is the intended solution. Regex is tempting because it elegantly handles quoted delimiters, but it violates the stated constraint and adds parsing overhead.
- ✗
Use the split() method and then merge fields that start with a double quote
Why it's wrong here
Splitting first then merging quoted fragments relies on guessing where a quoted field ends, and fails when a field contains several commas or adjacent quoted fields. It is tempting because it reuses split() and handles the sample line, and would suit data where quoted fields never contain more than one comma.
Go deeper
Related to this question
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 →
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.