简体   繁体   中英

How to search for a specific value in a .csv file using groovy

I have a .csv file for Unloco code and I want to search if a specific port code exists in the .csv file.

File:

ADALV,Andorra la Vella,,,4230N 00131E
ADCAN,Canillo,,,4234N 00135E

you can iterate all lines in your file with this kind of code:

def f = new File(myfile)
f.withReader("UTF-8"){ r->
    r.splitEachLine( ',' ){ line->
        if(line[0]=='ADCAN'){ 
            println "found: $line"
        }
    }
}

如果您只是想知道文件中存在代码,而又不关心在哪里可以执行以下操作,则text方法会将整个文件读取为字符串,但是如果处理大型文件,则效果不佳。

new File( 'myfile.csv' ).text.contains( 'ADCAN' )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM