JAVA中的CSV解析器,字符串中的双引号(SuperCSV,OpenCSV) [英] CSV parser in JAVA, double quotes in string (SuperCSV, OpenCSV)

查看:3779
本文介绍了JAVA中的CSV解析器,字符串中的双引号(SuperCSV,OpenCSV)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整天我一直在寻找如何解决这个问题,没有什么...
我想写函数,它将CSV文件转换为字符串列表的集合。
以下是此函数:

  public Collection<? extends List< String>> parse()throws IOException {
Collection< List< String>> collectionOfLists = new ArrayList< List< String>>();
CsvListReader parser = new CsvListReader(Files.newBufferedReader(pathToFile,StandardCharsets.UTF_8),CsvPreference.EXCEL_PREFERENCE);

List< String>行;
while((row = parser.read())!= null)
collectionOfLists.add(row);

return collectionOfLists;
}

public static String toString(Collection< ;? extends List< String>> csv){
StringBuilder builder = new StringBuilder
for(List< String> l:csv){
for(String s:l)
builder.append(s).append(',');
if(builder.length()> 0)
builder.setCharAt(builder.length() - 1,'\\\
');
}
return builder.toString();
}

  id,name,city,age 
1,Bob,London,12

toString(parse())的输出是:

  id,name,city,age 
1,Bob,London,12

$ b b

而不是相同的输入:/我能做什么,那些字符串包含\(引号)?
请帮助我。

数据包含引号 - 为什么会被删除?



在这种情况下,我会指向 CSV规范您的CSV文件未正确转义,因此这些引号实际上不是您的数据的一部分。



这应该是



1,Bob,伦敦,12



不是



1,Bob,伦敦,12



2。如何在写入时应用引号(即使数据不包含逗号,引号等)?



默认情况下,如果需要,Super CSV仅转义(该字段包含逗号,双引号或换行符)。



如果您真的要启用引号,则可以使用

例如,您可以在示例中使用以下首选项来引用name列:

p>

  private static final CsvPreference ALWAYS_QUOTE_NAME_COL = 
new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE)
.useQuoteMode(new ColumnQuoteMode (2))。build();

或者,如果要引用所有内容,则可以使用 AlwaysQuoteMode ,或者如果你想要一个完全自定义的解决方案,那么你可以写自己的 QuoteMode


all day I've been searching how to resolve this probem and nothing... I want to write function, which convert CSV file to collection of lists (of strings). Here is this function:

public Collection<? extends List<String>> parse() throws IOException {
    Collection<List<String>> collectionOfLists = new ArrayList<List<String>>();
    CsvListReader parser = new CsvListReader(Files.newBufferedReader(pathToFile, StandardCharsets.UTF_8), CsvPreference.EXCEL_PREFERENCE);

    List<String> row;
    while( (row = parser.read()) != null)
        collectionOfLists.add(row);

    return collectionOfLists;
}

public static String toString(Collection<? extends List<String>> csv) {
    StringBuilder builder = new StringBuilder();
    for(List<String> l : csv) {
        for(String s : l)
            builder.append(s).append(',');
        if(builder.length() > 0)
            builder.setCharAt(builder.length()-1,'\n');
    }
    return builder.toString();
}

But e.g. for that input:

id, name, city, age
1,"Bob",London,12

Output for toString(parse()) is:

id, name, city, age
1,Bob,London,12 

instead of the same like input:/ What can I do, that strings contain \" (quotes) ? Please help me.

解决方案

It's not clear from your question whether you're asking....

1. My data contains quotes - why are they being stripped out?

In this case, I'd point you to the CSV specification as your CSV file is not properly escaped, so those quotes aren't actually part of your data.

It should be

1,""Bob"",London,12

not

1,"Bob",London,12

2. How do I apply quotes when writing (even if the data doesn't contain commas, quotes, etc)?

By default Super CSV only escapes if necessary (the field contains a comma, double quote or newline).

If you really want to enable quotes, then you can configure Super CSV with a quote mode.

For example, you could always quote the name column in your example with the following preferences:

private static final CsvPreference ALWAYS_QUOTE_NAME_COL = 
    new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE)
    .useQuoteMode(new ColumnQuoteMode(2)).build();

Alternatively, if you want to quote everything then you can use AlwaysQuoteMode, or if you want a completely custom solution, then you can write your own QuoteMode.

这篇关于JAVA中的CSV解析器,字符串中的双引号(SuperCSV,OpenCSV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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