关于Java分割命令解析Csv文件 [英] Regarding Java Split Command Parsing Csv File

查看:913
本文介绍了关于Java分割命令解析Csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面格式的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 );

Java:拆分以逗号分隔的字符串,但忽略逗号在引号中



当我运行这个CSV数据时,我得到的items.length打印为8.最后两个逗号在行尾的NEW后被忽略我想要split命令来拾取这些逗号和返回我的长度为10.它不拾取空逗号,如果它在结束,但它正在拾取它,如果它在字符串中间不知道我需要修改在split命令解决这个问题。 csv文件一个Text字段内容中的双引号可以重复(例如This account is alargeone)

解决方案

正则表达式没有问题。问题是 split 丢弃最后的空匹配:


这个方法的工作原理是通过调用
两个 - 使用
给定表达式的限制参数
为零的方法。尾随空字符串是
,因此不包括在
结果数组中。


一个解决方法是提供一个参数大于您在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:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆