I am trying to write a function that checks whether all delimiters in a .csv file are ";". My attempt was to use csv.Sniffer()
:
import csv
def check_data_validity(file):
sniffer=csv.Sniffer()
dialect = sniffer.sniff(file)
if dialect.delimiter != ';':
return False
Regardless of the file, I always get "False". I suppose one can do it by reading each line and check what the separators are, but I find this quite daunting for huge files.
An example of file in the raw format would be:
timestamp_1;timestamp_2;Value_1;Value_2;Value_3;Result
1509494402000;2017-11-01T00:00:02Z;292.05;;26.89;0.0;;;
1509494410000;2017-11-01T00:00:10Z;6.0;;4.0;1.0;;;
1509494412000;2017-11-01T00:00:12Z;29.23;;4.0;0.0
You can use the Sniffer and check, whether an Error is thrown.
import csv
def check_data_validity(file):
with open(file, newline = "") as csvfile:
try:
dialect = csv.Sniffer().sniff(csvfile.read(1024), delimiters = ";")
print("Delimiter is ;")
except:
print("Wrong Delimiter")