Check if file has a CSV format with Python

You could try something like the following, but just because you get a dialect back from csv.Sniffer really won’t be sufficient for guaranteeing you have a valid CSV document.

csv_fileh = open(somefile, 'rb')
try:
    dialect = csv.Sniffer().sniff(csv_fileh.read(1024))
    # Perform various checks on the dialect (e.g., lineseparator,
    # delimiter) to make sure it's sane

    # Don't forget to reset the read position back to the start of
    # the file before reading any entries.
    csv_fileh.seek(0)
except csv.Error:
    # File appears not to be in CSV format; move along

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)