Regarding Java Csv File syntax

I have a csv file in the following format.

H,"TestItems_20100107.csv",07/01/2010,20:00:00,"TT1198","MOBb","AMD",NEW,, 

I need the split command to ignore commas inside double quotes. Therefore, I used the split split command from an earlier publication. Paste the URL that I used for this command

 String items[] = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); System.out.println("items.length"+items.length); 

Java: comma delimited but ignoring commas in quotation marks

When I run the CSV data, I get items.length printed as 8. The last two commas at the end of the line after "NEW" are ignored. I want the split command to take these commas and return me the length as 10. It does not collect zero commas if it ends, but it lifts it if it is in the middle of the line. Not sure what I need to change in the split command to solve this problem. Also in the csv file Double quotes in the contents of the text field can be repeated (for example, "This account is a" big "one")

+3
java split csv
source share
2 answers

There is nothing wrong with regex. The problem is that split discards empty matches at the end:

This method works as if a separation method with two arguments with a given expression and a limit argument of zero. Empty blank lines are therefore not included in the resulting array.

The workaround is to provide an argument greater than the number of columns you expect in your CSV file:

  String[] tokens = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", 99); 
+9
source share

Today I ran into this problem and found a simpe solution for csv files: adding an extra field containing only one space at split time:

 (line + ", ").split(","); 

Thus, no matter how many consecutive empty fields can exist at the end of the csv file, split () will always return n + 1 fields

Session Example (using bsh)

 bsh % line = "H,\"TestItems_20100107.csv\",07/01/2010,20:00:00,\"TT1198\",\"MOBb\",\"AMD\",NEW,, bsh % System.out.println(line); H,"TestItems_20100107.csv",07/01/2010,20:00:00,"TT1198","MOBb","AMD",NEW,, bsh % String[] items = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); bsh % System.out.println(items.length); 8 bsh % items = (line + ", ").split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); bsh % System.out.println(items.length - 1 ); 10 bsh % 
0
source share

All Articles