Link to home
Start Free TrialLog in
Avatar of sunshine737
sunshine737

asked on

Checking the delimiter in a csv file

Hello Experts,
I have some csv files , in some csv files the values are separated by ' ,' and in some other csv files the values are separated by ' ; ' . I need a java code to check whether the csv file is separated by , or ; and if the csv file is separated by ' ; ' then i should be able to  change the separator from ' ; '  to  ' , ' in the csv file.

 Thanks............
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post some (not too much) sample data
I'd do it by checking to see if a ";" exists on every line...

if so, then I'd assume ";" is the delimiter, and you'd have to read the file in line by line and do:

    line = line.replaceAll( ";", "," ) ;

then save it back out again to a new file...
Of course, you may hit problems with data such as:

  1,"tim;yates",2

where a ";" is inside a string var, and "," is the REAL delimiter...

But that can't really be avoided without writing a whole CSV import program...
Avatar of sunshine737
sunshine737

ASKER

First i would like to check the type of the delimiter whether it is , or ; .
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here's a regular expression I found that seems to work. I don't fully like it because it doesn't do the right thing IMO with respect to quotes -- the quotes are returned in the parsed element instead of discarded. But it's close and it is delimiter agnostic for comma and semicolon:

        String regexp = "\\s*[,;]\\s*(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
        Pattern pattern = Pattern.compile(regexp);
        String[] values = pattern.split(line);

Regards,
Jim
Hi TimYates ,
Thanks for your help.