Java CSV多数据解析



我有字符串作为:

String cont = "[["START","1001","","","2014-07-15","Invoice",0,13.46,"1682432"]," +
                    "["START","1001","","","2014-07-15","Invoice",0,-13.46,"1682432"]," +
                    "["START","1001","","","2014-07-15","Invoice",0,-14.52,"1682432"]," +
                    "["START","6002","020","0000000PWO","2014-07-15","MY Comment - FICA and",-13.46,0,"1682432"]," +
                    "["START","6002","020","0000000PWO","2014-07-15","MY Comment - FEED",-1.06,0,"1682432"]" +
                "]";

我需要输出

Account || Date ||        Amount || Description ||          InvoiceNo
1001     2014-07-15       -13.46                            1682432
....some more data
6002     2014-07-15       -1.06    MY desc                  1682432

我正在尝试使用Apache CSV解析器与2.3版本。

<dependency>
    <groupId>net.sf.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>2.3</version>
</dependency>
java代码:

CSVReader reader = new CSVReader(new StringReader(cont), ',');
List<String[]> records = reader.readAll();
Iterator<String[]> iterator = records.iterator();
while (iterator.hasNext()) {
    String[] record  =  iterator.next();
    for (String string : record) {
        System.out.println(string);
    }
}
输出:

[[START 
1001  
2014-07-15
...
["START
6002
020
0000000PWO
2014-07-15
MY Comment - FEED
-1.06
0
1682432"]]

1)如何删除特殊字符"["one_answers"]"

2)如何给上面的输出字段赋值

我想把上面的csv转换成bean对象

您可以利用开放源码库univoc -parsers将csv转换为bean对象,如下面的代码所示:

public static void main(String[] args) throws FileNotFoundException {
    /**
     * ---------------------------------------------
     *  Read CSV rows into list of beans you defined
     * ---------------------------------------------
     */
    // 1st, config the CSV reader with row processor attaching the bean definition
    BeanListProcessor<ColumnBean> rowProcessor = new BeanListProcessor<ColumnBean>(ColumnBean.class);
    settings.setRowProcessor(rowProcessor);
    settings.setHeaderExtractionEnabled(true);
    // 2nd, parse all rows from the CSV file into the list of beans you defined
    parser.parse(new StringReader(cont));
    List<ColumnBean> resolvedBeans = rowProcessor.getBeans();
    // 3rd, process the beans with business logic
    // ......
}
使用该库,您只需要几行代码,并且它还提供了显著的性能。在其主页找到教程。

根据从bean对象到json的转换,您可以检查Google Gson项目

对于2),使用System.out.print,而不是println(这只是打印字符串,末尾没有新行),并将制表符(t)连接到您的数据值。要实现这一点,您需要将string变量分解为多个部分。制表符可以让你在正确的行中适当地对齐它们。

一旦字符串由多个部分组成,对于1),您将希望从第一个"片段"中切片第一个字符,并从最后一个片段中切片最后一个字符。这将删除第一个[和最后一个]。我建议使用substring .

为了删除特殊字符,我对所有特殊字符使用了regexp,除了字符串中似乎包含的"-"。如果要保留更多的字符,则将其重构为一个特殊的集合,并将其附加到regexp中。

对于赋值,我不确定你想做什么,我猜你想在代码中为一行或一行,ABCXYZ,一个字符串变量。如果您想从整行动态构造字符串,请使用StringBuilder或StringBuffer。我编写了这段代码来解析

String cont = "[["START","1001","","","2014-07-15","Invoice",0,13.46,"1682432"]," +
            "["START","1001","","","2014-07-15","Invoice",0,-13.46,"1682432"]," +
            "["START","1001","","","2014-07-15","Invoice",0,-14.52,"1682432"]," +
            "["START","6002","020","0000000PWO","2014-07-15","MY Comment - FICA and",-13.46,0,"1682432"]," +
            "["START","6002","020","0000000PWO","2014-07-15","MY Comment - FEED",-1.06,0,"1682432"]" +
            "]";
    CSVReader reader = new CSVReader(new StringReader(cont), ',');
    List<String[]> records = reader.readAll();
    Iterator<String[]> iterator = records.iterator();
    while (iterator.hasNext()) {
        String[] record = iterator.next();
        for (String string : record) {
            string = string.replaceAll("[^\w\s\-]", "");
            if (string.startsWith("START")) {
                System.out.println();
            }
            System.out.print(string);
            System.out.print(",");
        }
    }

您不需要使用任何csv解析器只需使用下面的代码

public static void main(String arg[]) {
   String cont = "[["START","1001","","","2014-07-15","Invoice",0,13.46,"1682432"]," +
                "["START","1001","","","2014-07-15","Invoice",0,-13.46,"1682432"]," +
                "["START","1001","","","2014-07-15","Invoice",0,-14.52,"1682432"]," +
                "["START","6002","020","0000000PWO","2014-07-15","MY Comment - FICA and",-13.46,0,"1682432"]," +
                "["START","6002","020","0000000PWO","2014-07-15","MY Comment - FEED",-1.06,0,"1682432"]" +
            "]";
    String[] csvArry = cont.split(",");      
    for (String value : csvArry) {
        value = value.replaceAll("[^\w\s\-]", "");
        System.out.println(value); 
    }
}

它会做你想做的一切。

使用https://github.com/CyborTronik/fluent-ssv将CSV转换为bean

您还可以通过实现LineParser并将其提供给SsvStreamBuilder来编写行解析器(在那里您将抛出不必要的字符)。

相关内容

  • 没有找到相关文章

最新更新