Validate A CSV File just logic is required irrespective of programming language

Hi,

I just need logic to validate the below data in CSV file as provided below

id,name,address,phone,country
1,test1,"2,street,lane",0878799988,india
2,test2,A block bangalore,9999999999,india
3,"test3","xyz,xyz",0878799988,"india"


There can be deliberately "," added by the user,but it is enclosed in quotes.

I need to validate each record row wise,how can i split the records so that the only comma separated values are outputted in an array.

I don't need to use any third party tool or open source for the above.

pseudo code provided is very helpful.
RockingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
What you might do programmatically would be dependent on what you mean by "validate."  In PHP, there is the fgetcsv() function that can read the CSV file one line at a time, automatically putting each of the elements into an array.  It is fairly straightforward, reading the CSV file until it encounters end-of-file.  With each array you can do any validation processing that makes sense for your application.
0
Chris StanyonWebDevCommented:
Here's a very simple example using PHP

//open the file for read only
$file = fopen("someFile.csv", "r");

while (!feof($file) ) {
     //read each line, separated by comma and enclosed in "
     $line = fgetcsv($file, 0, ',', '"');
     var_dump($line); //lets have a look at the data
}
fclose($file);
?>

Open in new window

Each line will be read into an array called $line, which can be indexed numerically $line[0], $line[1] etc. You can check each value against your validation rules in here, and decide what to do.
0
CEHJCommented:
You need to use a proper csv api - parsing csv is deceptively simple. In Java (there's no such thing as a non-language-specifc parser/validator), you should be looking at stuff like opencsv or ostermiller CsvParser
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

RockingAuthor Commented:
How abt if at first i need to validate if all the values in the row are provided i.e there are exact no of commas are there as per header?

Is there any function in php or how to it manually?

ex

1,test1,"2,street,lane",0878799988,india -> here there are 6 commas
2,test2,A block bangalore,9999999999,india -> here there are 4 commas

It should provide error as soon as it counts the no of commas should be 4 instead of 6?
0
Ray PaseurCommented:
Since PHP fgetcsv() returns an array, you can use count() to check the number of elements in the array.
0
CEHJCommented:
It's not really economical to validate a csv file. Validation is tantamount to parsing it, which is effectively processing it, so you might as well catch any exceptions that are thrown in the process of parsing it (assuming that's the goal). If there are any exceptions (of the appropriate sort), it has failed validation by definition.
0
RockingAuthor Commented:
@CEHJ
yes you are very true,but part of requirement :(
Is there any good parsers in java for validating  a  csv file against linux,unix,mac,windows since the line separator are different in OS?

@Ray_Paseur
Out put of my data in array would be as below
1,test1,"2,street,lane",0878799988,india ->
arr[0] = 1
arr[1] = test1
arr[2] = 2,street,lane
arr[3] = 0878799988
arr[4] = india

2,test2,A block bangalore,9999999999,india
arr[0] = 1
arr[1] = test1
arr[2] = A block
arr[3] = 9999999999
arr[4] = india

3,"test3","xyz,xyz",0878799988,"india"
arr[0] = 3
arr[1] = test3
arr[2] = xyz,xyz
arr[3] = 0878799988
arr[4] = india

Pls. confirm.
0
CEHJCommented:
yes you are very true,but part of requirement :(
Well, in that case, in Java, you'd just parse it and discard the data - problem solved. No exceptions == valid
0
RockingAuthor Commented:
Ok Can parse but can you pls provide a bit logic how to parse these lines since ", "is part of data therefore finding difficulty in parsing?

id,name,address,phone,country
1,test1,"2,street,lane",0878799988,india
2,test2,A block bangalore,9999999999,india
3,"test3","xyz,xyz",0878799988,"india"
0
CEHJCommented:
Why do you need 'logic'? Just use ... a csv parser
0
RockingAuthor Commented:
Why do you need 'logic'? Just use ... a csv parser
ahh i forgot (earlier mentioned by you) thanks a lot.
0
RockingAuthor Commented:
Coming to php

@Ray_Paseur
Out put of my data in array would be as below
1,test1,"2,street,lane",0878799988,india ->
arr[0] = 1
arr[1] = test1
arr[2] = 2,street,lane
arr[3] = 0878799988
arr[4] = india

2,test2,A block bangalore,9999999999,india
arr[0] = 1
arr[1] = test1
arr[2] = A block
arr[3] = 9999999999
arr[4] = india

3,"test3","xyz,xyz",0878799988,"india"
arr[0] = 3
arr[1] = test3
arr[2] = xyz,xyz
arr[3] = 0878799988
arr[4] = india

Pls. confirm.
0
Ray PaseurCommented:
The fgetcsv() function mentioned above performs the "parsing."

In your example, this segment is wrong:

2,test2,A block bangalore,9999999999,india
arr[0] = 1
arr[1] = test1
arr[2] = A block
arr[3] = 9999999999
arr[4] = india

It should be:

2,test2,A block bangalore,9999999999,india
arr[0] = 2
arr[1] = test2
arr[2] = A block bangalore
arr[3] = 9999999999
arr[4] = india

In PHP there is a context-aware end of line character known by the constant name PHP_EOL.  It is not part of this exercise because it is only needed when you're writing a file, not when you're reading it.

If you want to get some kind of a foundation in how PHP works, this article will help you find good learning resources and more importantly will keep you away from bad learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Is there any good parsers in java for validating  a  csv file against linux,unix,mac,windows since the line separator are different in OS?

No Java parser would likely be foxed by line separators since the principal base IO classes in Java are not foxed by them. The tricky stuff is quoting and escapement
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.