Parse CSV with Custom Delimiter and Quote Character in Python

Reads a CSV string with a custom delimiter and quote character using the csv module, returning a list of rows.

Easy Python 3.9+ Aug 9, 2026 Files & data 13 views 0 copies

Python code

12 lines
Python 3.9+
import csv
from io import StringIO

def parse_csv(data, delimiter='|', quotechar='"'):
    reader = csv.reader(StringIO(data), delimiter=delimiter, quotechar=quotechar)
    rows = [row for row in reader]
    return rows

if __name__ == "__main__":
    sample = 'Alice|"Smith, Jr."|25\nBob|"Johnson, Sr."|30'
    result = parse_csv(sample)
    print(result)

Output

stdout
[['Alice', 'Smith, Jr.', '25'], ['Bob', 'Johnson, Sr.', '30']]

How it works

The csv.reader object handles fields that contain the delimiter when they are wrapped in the quote character. StringIO turns the input string into a file-like object that the reader consumes interactively. The list comprehension iterates over all rows, yielding each as a list of parsed field values. Because both the delimiter and quote character are parameters, the same function adapts to pipe-separated or tab-separated data with minimal changes.

Common mistakes

  • Passing a raw string instead of a file-like object to csv.reader.
  • Forgetting to set `quotechar` when fields contain commas or the delimiter.
  • Assuming the reader returns a list automatically; it returns an iterator.
  • Not accounting for newline handling in multiline quoted fields.

Variations

  1. Use `csv.DictReader` to get rows as dictionaries keyed by a header row.
  2. Read directly from a file on disk: `with open('data.csv') as f: rows = list(csv.reader(f, delimiter='|'))`.

Real-world use cases

  • Importing legacy data dumps from mainframe systems that use pipe-separated values with quoted fields.
  • Processing server logs where messages contain commas and use a custom separator like semicolons.
  • Building an ETL job that ingests vendor files with inconsistent delimiters into a unified format.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.