CSV
Process custom PDF files.
Introduction
Netuno generates CSV files through the CSV feature, which provides a low-code abstraction of Apache Commons CSV for the various programming languages supported by Netuno.
The application that comes with Netuno called demo (demonstration application) contains many examples of possible code implementations in various languages, including the demonstration of export to CSV through the csv service located at:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
netuno/apps/demo/server/services/samples/javascript/csv.js
netuno/apps/demo/server/services/samples/typescript/csv.ts
netuno/apps/demo/server/services/samples/python/csv.py
netuno/apps/demo/server/services/samples/ruby/csv.rb
netuno/apps/demo/server/services/samples/kotlin/csv.kts
netuno/apps/demo/server/services/samples/groovy/csv.groovy
Create a CSV file
To create a CSV file, first you need to initialize a CSVPrinter from Apache Commons CSV.
To do this, we need to pass the folder name and the file name, as well as the CSV format.
In the example, we will generate the file inside the application's storage:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const csvPrinter = _csv.printer(
_storage.filesystem("server", "data.csv"),
_csv.format("EXCEL")
)
const csvPrinter = _csv.printer(
_storage.filesystem("server", "data.csv"),
_csv.format("EXCEL")
)
csv_printer = _csv.printer(
_storage.filesystem("server", "data.csv"),
_csv.format("EXCEL")
)
csv_printer = _csv.printer(
_storage.filesystem("server", "data.csv"),
_csv.format("EXCEL")
)
val csvPrinter = _csv.printer(
_storage.filesystem("server", "data.csv"),
_csv.format("EXCEL")
)
def csvPrinter = _csv.printer(
_storage.filesystem("server", "data.csv"),
_csv.format("EXCEL")
)
The supported formats are described here.
Writing
To write rows to the CSV file:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
csvPrinter.printRecord("id", "userName", "firstName", "lastName", "birthday")
csvPrinter.printRecord(1, "john73", "John", "Doe", 123)
csvPrinter.printRecord("id", "userName", "firstName", "lastName", "birthday")
csvPrinter.printRecord(1, "john73", "John", "Doe", 123)
csvPrinter.printRecord("id", "userName", "firstName", "lastName", "birthday")
csvPrinter.printRecord(1, "john73", "John", "Doe", 123)
csvPrinter.printRecord("id", "userName", "firstName", "lastName", "birthday")
csvPrinter.printRecord(1, "john73", "John", "Doe", 123)
csvPrinter.printRecord("id", "userName", "firstName", "lastName", "birthday")
csvPrinter.printRecord(1, "john73", "John", "Doe", 123)
csvPrinter.printRecord("id", "userName", "firstName", "lastName", "birthday")
csvPrinter.printRecord(1, "john73", "John", "Doe", 123)
Closing
At the end, you need to close the CSVPrinter:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
csvPrinter.close()
csvPrinter.close()
csvPrinter.close()
csvPrinter.close()
csvPrinter.close()
csvPrinter.close()
Read a CSV file
To read a CSV file, first you need to initialize a CSVParser from Apache Commons CSV.
To do this, we need to pass the folder name and the file name.
In the example, we will read the file inside the application's storage:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
const csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
val csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
final csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
Rows
Now it is possible to iterate over the CSVRecord from Apache Commons CSV, which represent each row of the CSV file:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
for (const record of csvParser) {
const columnOne = record.get(0)
const columnTwo = record.get(1)
}
for (const record of csvParser) {
const columnOne = record.get(0)
const columnTwo = record.get(1)
}
for record in csvParser:
columnOne = record.get(0)
columnTwo = record.get(1)
csvParser.each do |record|
columnOne = record.get(0)
columnTwo = record.get(1)
end
for (record in csvParser) {
val columnOne = record.get(0)
val columnTwo = record.get(1)
}
for (record in csvParser) {
def columnOne = record.get(0)
def columnTwo = record.get(1)
}
Or get each row with the getRecord method from Apache Commons CSV.
In this example, the first get gets the first row. The second get gets the first field/value of that row:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
csvParser.getRecords().get(0).get(0)
csvParser.getRecords().get(0).get(0)
csvParser.getRecords().get(0).get(0)
csvParser.getRecords().get(0).get(0)
csvParser.getRecords().get(0).get(0)
csvParser.getRecords().get(0).get(0)
Closing
At the end, you need to close the CSVParser:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
csvParser.close()
csvParser.close()
csvParser.close()
csvParser.close()
csvParser.close()
csvParser.close()