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.
Python code
12 linesimport 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
[['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
- Use `csv.DictReader` to get rows as dictionaries keyed by a header row.
- 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
More from Files & data
- Append a Line to a Log File in Python easy
- Audit File Permissions Across a Project in Python easy
- Automatically Detect Corrupted Files Using SHA-256 Checksums in Python easy
- Automatically Highlight Data Validation Errors Inside Excel Files in Python easy
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a File Index by Relative Path Hash Map in Python easy
Keep learning
Related tutorials and quizzes for this topic.