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............
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............
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...
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...
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...
ASKER
First i would like to check the type of the delimiter whether it is , or ; .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
String regexp = "\\s*[,;]\\s*(?=(?:[^\"]*\
Pattern pattern = Pattern.compile(regexp);
String[] values = pattern.split(line);
Regards,
Jim
ASKER
Hi TimYates ,
Thanks for your help.
Thanks for your help.
:-)