Get the separator output by read_csv in pandas

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

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

EDIT

I am looking specifically for a method that uses the pandas object that is returned read_csv. I am using version 0.20.2 from pandas.

+4
source share
3 answers

I think you can do this without importing csv:

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

EDIT:

Forgot the argument iterator = True.

+4
source

, , csv ( ), csv.Sniffer :

Sniffer CSV .

, sniff:

sniff(sample, delimiters=None)

Dialect, . , , .

:

with open('example.csv', 'r') as csvfile:
    dialect = csv.Sniffer().sniff(csvfile.read(1024))
    print(dialect.delimiter)
+3

csv.Sniffer

Sniffer CSV .

sniff (sample, delimiters = None)

, . , .


Dialect.delimiter

, . - ','

import csv

sniffer = csv.Sniffer()
dialect = sniffer.sniff('first, second, third, fourth')
print dialect.delimiter
0

Source: https://habr.com/ru/post/1683882/


All Articles