Skip to content

Instantly share code, notes, and snippets.

@psibre
Created March 22, 2017 15:17
Show Gist options
  • Save psibre/5b469f06b3e740ce1a52e344930c679a to your computer and use it in GitHub Desktop.
Save psibre/5b469f06b3e740ce1a52e344930c679a to your computer and use it in GitHub Desktop.
Convert CSV to YAML with Gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.xlson.groovycsv:groovycsv:1.1'
classpath 'org.yaml:snakeyaml:1.18'
}
}
task csv2yaml {
def csvFile = file('utterances.csv') // CSV should contain tmin and tmax columns
def yamlFile = file('utterances.yaml')
inputs.files csvFile
outputs.files yamlFile
doLast {
def csv = com.xlson.groovycsv.CsvParser.parseCsv(csvFile.newReader()).collect {
[start: it.tmin as float, end: it.tmax as float]
}
def opts = new org.yaml.snakeyaml.DumperOptions()
opts.defaultFlowStyle = org.yaml.snakeyaml.DumperOptions.FlowStyle.BLOCK
def yaml = new org.yaml.snakeyaml.Yaml(opts)
yaml.dump(csv, yamlFile.newWriter())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment