将Groovy csv转换为字符串 [英] Groovy csv to string

查看:217
本文介绍了将Groovy csv转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Dell Boomi将数据从一个系统映射到另一个系统.我可以在地图中使用groovy,但没有经验.我尝试使用其他Boomi工具执行此操作,但被告知我需要在脚本中使用groovy.我的入站数据是:

I am using Dell Boomi to map data from one system to another. I can use groovy in the maps but have no experience with it. I tried to do this with the other Boomi tools, but have been told that I'll need to use groovy in a script. My inbound data is:

132265,棕色

132265,Brown

132265,金

132265,灰色

132265,绿色

我想输出:

132265,棕色,金色,灰色,绿色"

132265,"Brown,Gold,Gray,Green"

希望这是有道理的! groovy代码上有什么想法可以实现这项工作吗?

Hopefully this makes sense! Any ideas on the groovy code to make this work?

推荐答案

可以使用

It can be elegantly solved with groupBy and the spread operator:

@Grapes(
    @Grab(group='org.apache.commons', module='commons-csv', version='1.2')
)

import org.apache.commons.csv.*

def csv = '''
132265,Brown
132265,Gold
132265,Gray
132265,Green
'''

def parsed = CSVParser.parse(csv, CSVFormat.DEFAULT.withHeader('code', 'color')
parsed.records.groupBy({ it.code }).each { k,v -> println "$k,\"${v*.color.join(',')}\"" }

上面的照片:

132265,"Brown,Gold,Gray,Green"

这篇关于将Groovy csv转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆