Retrieve delimiter infered by read_csv in pandas

后端 未结 3 764
無奈伤痛
無奈伤痛 2021-02-13 04:18

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 separat

3条回答
  •  生来不讨喜
    2021-02-13 05:10

    csv.Sniffer

    The Sniffer class is used to deduce the format of a CSV file.

    sniff(sample, delimiters=None)

    Analyze the given sample and return a Dialect subclass reflecting the parameters found. If the optional delimiters parameter is given, it is interpreted as a string containing possible valid delimiter characters.


    Dialect.delimiter

    A one-character string used to separate fields. It defaults to ','

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

提交回复
热议问题