How can we validate a CSV file ?
I have an CSV file of structure:
Date;Id;Shown
15-Mar-10;231;345
15-Mar-10;232;346
and so on and on !!! approx around 80,000 rows.
How can I validate this CSV file before starting the parsing using fgetcsv
?
if we want to validate each row of csv file we can validate for example. var csvStream = fs. createReadStream("Sample. csv"); csv .
Just enter the location of the file you want to check, or upload it. If you have a schema which describes the contents of the CSV file, you can also give its URL or upload it. CSVLint currently only supports validation of delimiter-separated values (dsv) files.
I.e., if you want to validate data from a CSV file, you have to first construct a CSV reader using the standard Python csv module, specifying the appropriate dialect, and then pass the CSV reader as the source of data to either the CSVValidator. validate or the CSVValidator. ivalidate method.
A CSV file must contain every column for all attributes of its object type. If the attribute value is not required, a column must exist with the attribute name and a comma for any blank attribute values. If a column is defined as optional, it means that the column is required to exist, but the value can be blank.
I would not try to validate the file before hand : I would rather prefer going through it line by line, dealing with each line separately :
Now, what could "verify it's OK" means ?
fgetcsv
, maybe some other function specific to my project -- anyway, if I cannot read one line with my function that reads hundreds, it's probably because there's a problem on that line)
If all that goes OK -- well, not much more to do, excepts use the data ;-)
And when you're done with one line, just go repeat for the next one.
Of course, if you want to either accept or reject a whole file before doing any database (or anything like that) write, you'll have to :
In your specific case, you have three kind of fields :
Date;Id;Shown
15-Mar-10;231;345
15-Mar-10;232;346
From what I can guess :
strtotime
(not sure it's ok for the format you're using, though)
explode
the string
Jan
, Feb
, Mar
, ...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