Could someone provide an effective way to check if a file has CSV format using Python ?
Count the number of commas in the file per line, there should normally be the same amount of commas on each line of the file for it to be a valid CSV file.
The general way of recognizing the type of file is by looking at its extension. But this isn't generally the case. This type of standard for recognizing file by associating an extension with a file type is enforced by some operating system families (predominantly Windows).
DictReader() class can be used to read a CSV file as a dictionary.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With