Multiple delimiters in single CSV file

前端 未结 3 370
情深已故
情深已故 2020-12-10 18:22

I have a CSV, which has got three different delimiters namely, \'|\', \',\' and \';\' between different columns.

How can I using Python parse this CSV ?

My

3条回答
  •  半阙折子戏
    2020-12-10 18:43

    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:

提交回复
热议问题