Groovy GroupBy field with and without white spaces

前端 未结 2 624
轮回少年
轮回少年 2020-12-12 05:56

I have invoices list as below

def invoices = [
\'LEDES98BI V2\',
\'LINE|INVOICE_DATE|INVOICE_NUMBER|INVOICE_TOTAL\',
\'1|20150301|INV-Error_Test1|22\',
\'2|2         


        
相关标签:
2条回答
  • 2020-12-12 06:02

    You just need to quote your name, like this

    def invoiceMap = lines.tail().collect{ [heads, it].transpose().collectEntries() }.groupBy{ it.'INVOICE NUMBER' }
    
    0 讨论(0)
  • 2020-12-12 06:22

    Use a proper CSV parser, rather than rolling your own.

    @Grab('com.xlson.groovycsv:groovycsv:1.0')
    import static com.xlson.groovycsv.CsvParser.parseCsv
    
    def invoices = [
    'LEDES98BI V2',
    'LINE|INVOICE_DATE|INVOICE_NUMBER|INVOICE_TOTAL',
    '1|20150301|INV-Error_Test1|22',
    '2|20150301|INV-Error_Test1|24',
    '3|20150301|INV-Error_Test2|26',
    '4|20150301|INV-Error_Test2|28,']
    
    def data = parseCsv(invoices.drop(1).join('\n'), separator:'|')
    def invoiceMap = data.collect().groupBy { it.INVOICE_NUMBER }
    

    Or with a space in the column title:

    def invoices = [
    'LEDES98BI V2',
    'LINE|INVOICE_DATE|INVOICE NUMBER|INVOICE_TOTAL',
    '1|20150301|INV-Error_Test1|22',
    '2|20150301|INV-Error_Test1|24',
    '3|20150301|INV-Error_Test2|26',
    '4|20150301|INV-Error_Test2|28,']
    
    def data = parseCsv(invoices.drop(1).join('\n'), separator:'|')
    def invoiceMap = data.collect().groupBy { it.'INVOICE NUMBER' }
    
    0 讨论(0)
提交回复
热议问题