Multiple delimiters in single CSV file

Sticking with the standard library, re.split() can split a line at any of these characters:

import re

with open(file_name) as fobj:
    for line in fobj:
        line_data = re.split('Delim_first|Delim_second|[|]', line)
        print(line_data)

This will split at the delimiters |, Delim_first, and Delim_second.

Or with pandas:

import pandas as pd
df = pd.read_csv('multi_delim.csv', sep='Delim_first|Delim_second|[|]', 
                  engine="python", header=None)

Result:

enter image description here

Leave a Comment