Created
December 1, 2021 11:36
-
-
Save chumpa/8352b71d8d47c96f4f56c065eb7fe115 to your computer and use it in GitHub Desktop.
CSV parser on CPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.csv.CSVParser | |
import org.apache.commons.csv.CSVRecord | |
import org.apache.commons.csv.CSVFormat | |
@Test | |
void csv() { | |
// в CPI лучше получать не стринг а сразу Reader | |
String body = """"a","b" | |
"c","d" | |
"1","2" | |
""" | |
StringReader sr = new StringReader(body) | |
CSVParser parser = new CSVParser(sr, CSVFormat.EXCEL.withHeader()) | |
try { | |
for (final CSVRecord record : parser) { | |
String a = record.get("a") | |
String b = record.get("b") | |
println("$a\t$b") | |
} | |
} finally { | |
parser.close(); | |
sr.close(); | |
} | |
} | |
/* | |
results: | |
c d | |
1 2 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment