CSV NOT A CSV that has commas in the text / fields / coloumns

We've all seen them, so called CSV files that have commas in the actual data! That means they are NOT CSV files doesn't it??

This is a tricky one for most people....Thank fully I'm not most people ;) I've read dozens of ways using complicated code (sometimes hundreds of lines there of) and to this day the simplest way to deal with these NOT CSV files is to take a step back from your degree in computer science and think like a child!

"Col One","Col Two","Col Three","Col, Four"

You can see the problem there, as far a we are concerned its got four columns, splitting this line up on the commas (as we are supposed to do with REAL CSV) won't work will it? So to solve the problem we split on multiple characters EG;

(in perl): my ($col1,$col2,$col3,$col4) = split('","', $line);
(in php(to an array)): $array = explode('","', $line);
(in java): String[] parts = yourString.split('","')

It's not perfect and you will need to remove a leading and trailing " nor does it work on NOT CSV " encapsulated text but ... Job Done(I hope :)!!!


Add Comment

Poster Name / Handle / Email:

Comment

Add the words "wont cat"


Back