Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve delimiter infered by read_csv in pandas

Tags:

python

pandas

csv

When using the configuration for automatic separator detection to read csv files (pd.read_csv(file_path, sep=None)), pandas tries to infer the delimiter (or separator).

Is there a way to retrieve the result of this inference (the value that was finally used for sep)?

EDIT

I am looking specifically for a method that uses the pandas object that is returned by read_csv. I use version 0.20.2 of pandas.

like image 656
Leo Bouloc Avatar asked Aug 17 '17 10:08

Leo Bouloc


People also ask

What is delimiter in pandas read_csv?

read_csv(filepath_or_buffer, sep=', ', delimiter=None, header='infer', names=None, index_col=None, ....) It reads the content of a csv file at given path, then loads the content to a Dataframe and returns that. It uses comma (,) as default delimiter or separator while parsing a file.

How do I read a CSV file in a different delimiter?

Using the "From Text" feature in Excel Select the CSV file that has the data clustered into one column. Select Delimited, then make sure the File Origin is Unicode UTF-8. Select Comma (this is Affinity's default list separator). The preview will show the columns being separated.

Which argument in the read_csv method is used to specify the delimiter?

read_csv() method. We have to import pandas library to use this method. This method uses comma ', ' as a default delimiter but we can also use a custom delimiter or a regular expression as a separator.


1 Answers

I think you can do this without having to import csv:

reader = pd.read_csv(file_path, sep = None, iterator = True)
inferred_sep = reader._engine.data.dialect.delimiter

EDIT:

Forgot the iterator = True argument.

like image 195
RHSmith159 Avatar answered Oct 22 '22 23:10

RHSmith159