C.W.K.
Stream
Lesson 06 of 12 · published

CSV with Quoted Fields

~10 min · csv, parsing, quoting

Level 0Pattern-Curious
0 XP0/90 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

Real CSV is messy

Naive CSV is just str.split(','). Real-world CSV has commas inside quoted fields, escaped quotes inside quoted fields, embedded newlines, and Excel's bizarre dialect choices.

For real CSV processing, use a library — Python csv, JS papaparse, etc. They handle every dialect quirk.

When regex is OK

If your data is single-line, well-formed, and uses standard quoting, this regex extracts each field:

(?:"([^"]*)"|([^,]*))(?:,|$)

For each field: either a quoted string (capturing content) OR an unquoted run (no comma), followed by a comma or end of line.

Walking the line with finditer

Iterate matches; each one is a field. The two capture groups give either the quoted or unquoted form — pick whichever is non-None.

Embedded newlines — give up and use a library

If your CSV can have newlines inside quoted fields (Excel does this), regex isn't enough. The CSV grammar becomes context-sensitive. Use the csv module.

Code

Single-line CSV with quotes·python
import re

CSV_LINE = re.compile(r'(?:"([^"]*)"|([^,]*))(?:,|$)')

def parse_csv_line(line):
    fields = []
    for m in CSV_LINE.finditer(line):
        quoted, bare = m.groups()
        fields.append(quoted if quoted is not None else bare)
        if not m.group():
            break
    return fields

parse_csv_line('Pippa,"3, going on 30",warm')
# ['Pippa', '3, going on 30', 'warm', '']

# For real CSV processing, use the library
import csv
import io

lines = '''name,age,mood
Pippa,"3, going on 30",warm
"Wankyu, C.W.K.",58,curious'''

for row in csv.DictReader(io.StringIO(lines)):
    print(row)
# {'name': 'Pippa', 'age': '3, going on 30', 'mood': 'warm'}
# {'name': 'Wankyu, C.W.K.', 'age': '58', 'mood': 'curious'}

External links

Exercise

Take a CSV file you have (export anything from a spreadsheet). Try parsing one line with your own regex, then with csv.reader. Find the first line where they disagree. That's the moment you've discovered why the standard library exists.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.