DEV Community

Judy
Judy

Posted on

Parse a csv file where field values are enclosed by quotation marks and contain carriage return #eg35

Here is a csv file. Its 3rd column contains strings of a special format – they are enclosed by double quotation marks and some occupy multiple rows (which means they contain carriage return).

Image description
Task: Use Java to reorganize the csv file as a standard two-dimensional table.

Image description
Write the following SPL statement:

=file(""data.csv"").import@tcoq().run(description=replace(description,""\n"",""""))

import() function parses the csv file as a two dimensional table; @t option enables importing the 1st row as column names, @c option enables using commas as the separator, @o option handles the unescaped carriage return in lines, and @q option enables removing quotation marks at both sides before parsing. run() function modifies the two dimensional table and return the modified table.

Read How to Call a SPL Script in Java to find how to integrate SPL into a Java application.

This is one of the problems on StackOverflow. You can click on it to see that the conventional solution is quite complicated, but the SPL approach is really simple and efficient.

SPL open source address

Top comments (0)