2015-06-07 19 views

回答

1

下面的代碼段將完成這項工作:

def txt='''foo,bar 
abcd,12345 
def,234567''' 

txt.split('\n').collect { it.split(',') }.transpose().collect { field -> field.max { it.size() } }*.size() 
0

最後,我用這個:

def csv = new File('./myfile.csv').text 

def max = [ ] as ArrayList 

csv.eachLine { line, count -> 

    def params = line.split(',') 

    // skip the header line 
    if (count > 0) 
    { 
     params.eachWithIndex() { p, index ->   
      if (p.length() > max[index]) { 
       max[index] = p.length() 
      } 
     } 
    } 
} 
println "Max length of fields: ${max}" 
+0

你可以使用'splitEachLine'而不是'eachLine'和後來的'split' – cfrick

相關問題