简体   繁体   中英

Java CSV Reader, reading remaining data

I have CSV data as following:

1,mm/dd/yy,"abc,def,"pqr",xyz"

I would like to have this parsed into 3 strings.

  1. 1

  2. mm/dd/yy

  3. all remaining data, in this case, "abc,def,"pqr",xyz"

I have tried several libraries, openCSV, javacsv etc. all of them seems to parse and tokenize last column as well. What I want is remaining data after second column as a single token.

Any ideas ?

You should update the input data to enclose the 3rd column with single quote, like the following: 1,mm/dd/yy,'abc,def,"pqr",xyz'

Otherwise, you will never resolve the csv data correctly.

With the updated data, you can call the powerful open source library uniVocity-parsers to read the data correctly in just several lines:

public static void main(String[] args) throws FileNotFoundException {
    // 1st, config the CSV reader
    CsvParserSettings settings = new CsvParserSettings();
    settings.getFormat().setLineSeparator("\n");
    settings.getFormat().setQuote('\'');        // set the quote to single quote '
    settings.getFormat().setQuoteEscape('\\');  // escape the double quote "

    // 2nd, creates a CSV parser with the configs
    CsvParser parser = new CsvParser(settings);

    // 3rd, parses all rows from the CSV file into a 2-dimensional array
    List<String[]> resolvedData = parser.parseAll(new StringReader("1,mm/dd/yy,'abc,def,\"pqr\",xyz'"));
    for (String[] row : resolvedData) {
        StringBuilder strBuilder = new StringBuilder();
        for (String col : row) {
            strBuilder.append(col).append("\t");
        }
        System.out.println(strBuilder);
    }
}

And you will get output like this:

1 mm/dd/yy abc,def,"pqr",xyz

int firstCommaIndex = s.indexOf(',');
int secondCommaIndex = s.indexOf(',', firstCommaIndex + 1);
String firstPart = s.substring(0, firstCommaIndex);
String secondPart = s.substring(firstCommaIndex + 1, secondCommaIndex);
String lastPart = s.substring(secondCommaIndex + 1);

Try SuperCSV . It has quoteChar configuration option which seems to express handling of quoted text.

You can use custom LineParser on https://github.com/CyborTronik/fluent-ssv

And it as well will transform your CSV data into beans, but for date type you will need to provide a custom implementation of ValueConverter, otherwise you can store it as string and then manipulate it.

So code will look like:

new SsvStreamBuilder<MyBean>()
  .forEntity(MyBean.class)
  .withLineParser(new MyLineParser())
  .withValueConverter(new MyDateConverter())
  .stream("~/some/csv/file");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM