Search code examples
pythonpandascsvdelimiter

Auto-detect the delimiter in a CSV file using pd.read_csv


Is there a way for read_csv to auto-detect the delimiter? numpy's genfromtxt does this.

My files have data with single space, double space and a tab as delimiters. genfromtxt() solves it, but is slower than pandas' read_csv.

Any ideas?


Solution

  • Option 1

    Using delim_whitespace=True

    df = pd.read_csv('file.csv', delim_whitespace=True)
    

    Option 2

    Pass a regular expression to the sep parameter:

    df = pd.read_csv('file.csv', sep='\s+')
    

    This is equivalent to the first option


    Documentation for pd.read_csv.