Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing double quote from csv using jackson-dataformat-csv

I have a csv that contains the following

EANHotelID|SequenceNumber|Name|Address1|Address2|City|StateProvince|PostalCode|Country|Latitude|Longitude|AirportCode|PropertyCategory|PropertyCurrency|StarRating|Confidence|SupplierType|Location|ChainCodeID|RegionID|HighRate|LowRate|CheckInTime|CheckOutTime
541454|99999999|Hotel Maan Residency|"B" Wing, Gopal Palace, opp. ocean park|Naherunagar-Satellite Road|Ahmedabad||380 015|IN|23.02266|72.53842|AMD|1|INR|3.0||ESR|Near Kankaria Lake|||0|0|10:00 AM|10:00 AM

Now I'm trying to read each row in this csv as an object using the following code

    CsvMapper mapper = new CsvMapper();
    mapper.findAndRegisterModules();

    File csvFile = new File("D:\\ActivePropertyList.txt.bak2");

    CsvSchema schema = CsvSchema.emptySchema().withHeader().withColumnSeparator('|').withNullValue("");
    MappingIterator<Map<String,String>> it = mapper.readerFor(Map.class).with(schema)
                .readValues(csvFile);

    while (it.hasNextValue()) {
        Map<String,String> value = it.nextValue();
    }

But it's failing because of the "B" that exists in the csv. I get the following error:

Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('W' (code 87)): Expected separator ('"' (code 34)) or end-of-line at [Source: (com.fasterxml.jackson.dataformat.csv.impl.UTF8Reader); line: 2, column: 43]

How can I parse the double quotes in the csv correctly? I tried playing around with schema.withEscapeChar() schema.withQuoteChar() but I couldn't get it to work.

like image 883
prettyvoid Avatar asked Nov 29 '17 12:11

prettyvoid


1 Answers

Use withoutQuoteChar() on the schema to handle double quotations in the csv content, i.e.

CsvSchema.emptySchema().withHeader().withColumnSeparator('|').withNullValue("").withoutQuoteChar();
like image 117
prettyvoid Avatar answered Oct 18 '22 11:10

prettyvoid