Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Groovy读取CSV文件并将结果放入映射中(不使用任何外部库)_Groovy - Fatal编程技术网

使用Groovy读取CSV文件并将结果放入映射中(不使用任何外部库)

使用Groovy读取CSV文件并将结果放入映射中(不使用任何外部库),groovy,Groovy,我有一个CSV文件,如下所示: Sno、服务、操作、响应限制 1,建议服务,向上插入,50 2,ScheduleService,getReservation,10 3,预约服务,预约,23 我想将此结构作为地图列表,如下所示: [ [Sno:“1”、服务:“ProposalService”、操作:“upsert”、响应限制:“50”], [Sno:“2”、服务:“ScheduleService”、操作:“getReservation”、响应时间限制:“10”], [Sno:“3”、服务:“Sch

我有一个CSV文件,如下所示:

Sno、服务、操作、响应限制
1,建议服务,向上插入,50
2,ScheduleService,getReservation,10
3,预约服务,预约,23

我想将此结构作为地图列表,如下所示:

[
[Sno:“1”、服务:“ProposalService”、操作:“upsert”、响应限制:“50”], [Sno:“2”、服务:“ScheduleService”、操作:“getReservation”、响应时间限制:“10”], [Sno:“3”、服务:“ScheduleService”、操作:“bookAppointment”、响应限制:“23”]
]


我使用CSVReader(OpenCSV)获得了解决方案。但是我可以在不使用任何外部引用/库(如openCSV等)的情况下执行此操作吗?

如果CSV文件的结构始终相同,您可以使用Groovy脚本执行此操作:

def mapList = []

File csvFile = new File("/path/to/your/file.csv")

csvFile.eachLine { line ->
    def parts = line.split(",")
    def tmpMap = [:]

    tmpMap.putAt("Sno", parts[0])
    tmpMap.putAt("Service", parts[1])
    // etc.

    mapList.add(tmpMap)
}

如果CSV文件的结构始终相同,则可以使用Groovy脚本:

def mapList = []

File csvFile = new File("/path/to/your/file.csv")

csvFile.eachLine { line ->
    def parts = line.split(",")
    def tmpMap = [:]

    tmpMap.putAt("Sno", parts[0])
    tmpMap.putAt("Service", parts[1])
    // etc.

    mapList.add(tmpMap)
}

下面是迭代csv文件所有行的另一种方法:

def mapList = []
def headers = []
new File("/path/to/your/file.csv").readLines().eachWithIndex { row, rowIndex ->
    if (rowIndex == 0) { headers = row.split(',') }
    else {
        def tmpMap = [:]
        def cells = row.split(',').eachWithIndex { cell, cellIndex ->
          tmpMap[headers[cellIndex]] = cell
        }
        mapList.add(tmpMap)
    }
}
地图列表结果:

[[Sno:1, Service:ProposalService, Operation:upsert, ResponseTimeLimit:50], [Sno:2, Service:ScheduleService, Operation:getReservation, ResponseTimeLimit:10], [Sno:3, Service:ScheduleService, Operation:bookAppointment, ResponseTimeLimit:23]]
[Finished in 1.647s]`

下面是迭代csv文件所有行的另一种方法:

def mapList = []
def headers = []
new File("/path/to/your/file.csv").readLines().eachWithIndex { row, rowIndex ->
    if (rowIndex == 0) { headers = row.split(',') }
    else {
        def tmpMap = [:]
        def cells = row.split(',').eachWithIndex { cell, cellIndex ->
          tmpMap[headers[cellIndex]] = cell
        }
        mapList.add(tmpMap)
    }
}
地图列表结果:

[[Sno:1, Service:ProposalService, Operation:upsert, ResponseTimeLimit:50], [Sno:2, Service:ScheduleService, Operation:getReservation, ResponseTimeLimit:10], [Sno:3, Service:ScheduleService, Operation:bookAppointment, ResponseTimeLimit:23]]
[Finished in 1.647s]`