DEV Community

Judy
Judy

Posted on

2 1 1 1 1

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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay