Maria Fernanda
Maria Fernanda

Reputation: 195

Python detection of delimiter/separator in a csv file

I have a function that reads and handles *.csv files in several dataframes.

However, not all of the .csv files have the same separator. Is there a way to detect which type of separator the .csv file has, and then use it in the Pandas' read_csv() function?

df = pd.read_csv(path, sep = 'xxx',header = None, index_col = 0)

Upvotes: 8

Views: 9810

Answers (2)

natielle
natielle

Reputation: 426

As Reda El Hail said in comments before, set the param sep = None, like this:

pandas.read_csv('data.csv',sep=None)

And if you use the lib awswrangler to read csv files in s3, you can do similar to this:

awswrangler.s3.read_csv('s3://bucket/prefix', sep = None)

Upvotes: 4

Corralien
Corralien

Reputation: 120469

Update

In fact, use engine='python' as parameter of read_csv. It will try to automatically detect the right delimiter.

sepstr, default ‘,’

Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\r\t'.

Use csv.Sniffer:

import csv

def find_delimiter(filename):
    sniffer = csv.Sniffer()
    with open(filename) as fp:
        delimiter = sniffer.sniff(fp.read(5000)).delimiter
    return delimiter

Demo:

>>> find_delimiter('data.csv')
','

>>> find_delimiter('data.txt')
' ' 

Upvotes: 15

Related Questions