Parse a csv file where field values are enclosed by quotation marks and contain carriage return
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).
id,name,description,age 23,Anna,"Self-made Chef Shoemaker",23 1,Lily,"One Way go far",24 2,Joe,"Go aHead",55 3,Bat,"A fried",41 |
Task: Use Java to reorganize the csv file as a standard two-dimensional table.
id |
name |
description |
age |
23 |
Anna |
Self-made Chef Shoemaker |
23 |
1 |
Lily |
One Way go far |
24 |
2 |
Joe |
Go aHead |
55 |
3 |
Bat |
A fried |
41 |
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.
Source:https://stackoverflow.com/questions/72844595/parsing-double-quote-with-new-line-from-csv-using-jackson-dataformat-csv
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL