问题描述
我有一个下面格式的csv文件。
H,TestItems_20100107.csv,07/01/2010 ,20:00:00,TT1198,MOBb,AMD,NEW ,,
我需要split命令忽略双引号内的逗号。所以我使用以下拆分命令从较早的帖子。粘贴我使用此命令的URL
String items [] = line.split(,(? \\] * \[^ \] * \)* [^ \] * $));
System.out.println(items.length+ items.length );
当我运行这个CSV数据时,我得到的items.length打印为8.最后两个逗号在行尾的NEW后被忽略我想要split命令来拾取这些逗号和返回我的长度为10.它不拾取空逗号,如果它在结束,但它正在拾取它,如果它在字符串中间不知道我需要修改在split命令解决这个问题。 csv文件一个Text字段内容中的双引号可以重复(例如This account is alargeone)
正则表达式没有问题。问题是丢弃最后的空匹配:
一个解决方法是提供一个参数大于您在CSV文件中预期的列数:
String [] tokens = line.split( =([^ \] * \[^ \] * \)* [^ \] * $),99);
/ pre>
I have a csv file in the below format.
H,"TestItems_20100107.csv",07/01/2010,20:00:00,"TT1198","MOBb","AMD",NEW,,
I require the split command to ignore the commas inside the double quotes . So i used the below split command from an earlier post. Pasted the URL that i took this command
String items[] = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); System.out.println("items.length"+items.length);
Java: splitting a comma-separated string but ignoring commas in quotes
When i run for this CSV data I am getting the items.length printed as 8. The last two commas at the end of line after "NEW" are ignored. I want the split command to pick up these commas and return me the length as 10. It's not picking up the null commas if it's in end but it's picking it up if it's in the middle of string. Not sure what i need to modify in the split command to resolve this issue. Also in the csv file Double quotes within the contents of a Text field can be repeated (e.g. "This account is a ""large"" one")
解决方案There's nothing wrong with the regular expression. The problem is that split discards empty matches at the end:
A workaround is to supply an argument greater than the number of columns you expect in your CSV file:
String[] tokens = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", 99);
这篇关于关于Java分割命令解析Csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!