CSV Dialect Sniffer & Custom Row Splitter
Naive `line.split(",")` fails on real-world CSV rows containing commas inside quoted strings (`"Chicago, IL"`) or escaped double quotes (`""`). A deterministic character state machine tracks quote boundaries, accumulating fields safely in a single $O(N)$ linear pass 📊📑⚡.
def split_csv(line, sep=","):
cols, cur, in_q = [], [], False
for ch in line:
if ch == '"': in_q = not in_q
elif ch == sep and not in_q:
cols.append("".join(cur)); cur = []
else: cur.append(ch)
cols.append("".join(cur))
return cols
raw = 'Alice,"Seattle, WA",Engineer'
print("Parsed Fields:", split_csv(raw))
# 8. CSV Dialect Sniffer & Custom Row Splitter Junior developers assume comma-separated values are as simple as `line.split(',')`. In reality, CSV files are full of messy edge cases: embedded commas, semicolons, tabs, newlines inside cells, and weird quote escaping rules 🛑. ```python # RFC 4180 CSV Rules: # 1. If a field contains a comma, the entire field MUST be enclosed in quotes: # 42,"New York, NY",100 # # 2. If a quoted field contains a literal quote, it MUST be doubled (""): # 42,"John ""Ace"" Doe",100 ``` ### The Character State Machine To parse high-throughput CSV streams safely without regex backtracking or third-party dependencies: - Maintain an `in_quotes` boolean flag. - Look ahead by one character when encountering `"`: if `row[i + 1] == '"'`, advance by 2 characters and output a single literal quote. - Only split on the delimiter if `not in_quotes` 📊📑⚡. --- ### Interactive Pop-Quiz 🧠#### How does the RFC 4180 CSV standard represent a literal double-quote character inside an already quoted field? - [ ] By escaping it with two consecutive double-quote characters (`""`) 📑 - [ ] By preceding it with a backslash (`\"`) - [ ] By replacing it with an HTML entity (`"`) - [ ] By enclosing it in curly braces (`{\"}`) --- ### Practical Challenge 🎯 Implement `parse_csv_row(row: str, delimiter: str = ',') -> list[str]` using a character loop state machine. Correctly handle: 1) Delimiters inside quotes, and 2) Escaped quotes (`""` within quotes producing a single `"`). Test with `'42,"John ""Ace"" Doe","New York, NY",95000'`, assert 4 parsed fields, and print the parsed list. <details> <summary>Solution</summary> ```python def parse_csv_row(row: str, delimiter: str = ",") -> list[str]: fields = [] current = [] in_quotes = False i = 0 while i < len(row): char = row[i] if char == '"': if in_quotes and i + 1 < len(row) and row[i + 1] == '"': current.append('"') i += 2 continue else: in_quotes = not in_quotes elif char == delimiter and not in_quotes: fields.append("".join(current)) current = [] else: current.append(char) i += 1 fields.append("".join(current)) return fields line = '42,"John ""Ace"" Doe","New York, NY",95000' fields = parse_csv_row(line) assert len(fields) == 4 assert fields[0] == "42" assert fields[1] == 'John "Ace" Doe' assert fields[2] == "New York, NY" assert fields[3] == "95000" print(f"CSV row parsed successfully: {fields}") ``` </details>