如何在Java中处理CSV数据中的逗号 [英] How to handle commas in the data of a csv in Java
问题描述
我正在使用scanner.delimiter来分割我的csv,分隔符为,".但是,我有一些数据在"Monsters,Inc."之类的数据中包含逗号.
I'm using scanner.delimiter to split my csv, with the delimiter being ",". However I have some data that includes commas in the data like "Monsters, Inc."
但是,如果我将定界符设置为"\",\",则它将在其他所有内容上崩溃.
However if I set the delimiter to "\",\"", then it crashes on everything else.
不需要我编写自己的scan.delimiter方法的想法吗?
Ideas that don't require me to write my own scanner.delimiter method?
推荐答案
我认为Scanner.delimiter无法解决此类问题. 如果数据中存在多余逗号的引号,则可以使用正则表达式或代码来解决此类问题,也可以使用类似答案/问题中提到的String.split. 如果您没有报价,那么实际上您无能为力.
I don't think scanner.delimiter will work for this kind of problem. If you have quotes in the data where the extra comas exist, you can either use a regular expression or code to solve this kind of problem using also the String.split as mentioned in similar answers/questions. If you don't have quotations, then there is really nothing you can do.
在stackoverflow上也有类似的例子. 例如,我认为这适用于您:
There have been similar examples on stackoverflow. For example I think this one applies to you:
拆分使用String.split()将带有引号作为文本定界符的csv文件
使用拆分
public static void main(String[] args) {
String s = "Sachin,,M,\"Maths,Science,English\",Need to improve in these subjects.";
String[] splitted = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
System.out.println(Arrays.toString(splitted));
}
使用自定义代码
public static ArrayList<String> customSplitSpecific(String s)
{
ArrayList<String> words = new ArrayList<String>();
boolean notInsideComma = true;
int start =0, end=0;
for(int i=0; i<s.length()-1; i++)
{
if(s.charAt(i)==',' && notInsideComma)
{
words.add(s.substring(start,i));
start = i+1;
}
else if(s.charAt(i)=='"')
notInsideComma=!notInsideComma;
}
words.add(s.substring(start));
return words;
}
这篇关于如何在Java中处理CSV数据中的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!