CSV
Process custom CSV files.
Introduction
Netuno generates CSV files through the CSV resource, which provides a low-code abstraction over Apache Commons CSV for the various programming languages supported by Netuno.
The application included with Netuno called demo (demonstration application) contains several examples of possible implementations in multiple programming languages, including a demonstration of CSV export through the csv service located at:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
netuno/apps/demo/server/services/samples/javascript/csv.js
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
Creating a CSV file
To create a CSV file, you first need to initialize a CSVPrinter from Apache Commons CSV.
To do this, you must provide the folder name and file name, and the CSV format.
In this example, the file will be generated inside the application's storage:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
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
- 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)
Closing
When finished, you must close the CSVPrinter:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
csvPrinter.close()
csvPrinter.close()
csvPrinter.close()
csvPrinter.close()
csvPrinter.close()
Reading a CSV file
To read a CSV file, you first need to initialize a CSVParser from Apache Commons CSV.
To do this, you must provide the folder name and the file name.
In this example, the file is read from the application's storage:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
const csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
const csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
const csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
const csvParser = _csv.parser(_storage.filesystem("server", "data.csv"))
Rows
You can now iterate through the CSVRecord objects from Apache Commons CSV, each representing a row in the CSV file:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
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)
}
Alternatively, you can retrieve each row using the getRecords method from Apache Commons CSV.
In this example, the first get retrieves the first row. The second get retrieves the first field/value of that row:
- JavaScript
- 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)
Closing
When finished, you must close the CSVParser:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
csvParser.close()
csvParser.close()
csvParser.close()
csvParser.close()
csvParser.close()