如何使用Groovy逐一读取csv文件数据并将其放入变量 [英] How to Read the csv file data one by one and put into variable Using Groovy

查看:237
本文介绍了如何使用Groovy逐一读取csv文件数据并将其放入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我面临的一个问题是.我想读取csv文件数据并将其逐个放入一个变量中,并且该变量我想分配下一个Http请求,让它说ids.csv文件,其中包含这样的值

Here i am facing One problem that. i want to read the csv file data and one by one put it into a variable and that variable i want to assign a next Http Request let say ids.csv file which consisting of values like this

23333
23334
23335
22336
23336

我正在使用Jsr223预处理程序代码:

I am using Jsr223 PreProcessor code:

def csvfile = new File('D:/datas/id/ids.csv')
def lines =csvfile.readLines()
lines.each { String line ->
  vars.put('Id_value', line.toString())
}

如果错了,如何用简单的代码做到这一点?

If it is wrong, how to do this with simple code?

推荐答案

您需要在 JMeter变量参考名称,您当前的代码将仅创建一个值为23336Id_value变量,您需要对其进行如下修改:

You need to add a some form of counter to your JMeter Variables reference names, your current code will create only one Id_value variable with the value of 23336, you need to amend it like:

def csvfile = new File('D:/datas/id/ids.csv')
def lines =csvfile.readLines()
lines.eachWithIndex {line, idx ->
    vars.put('Id_value_' + idx, line)
}

您将获得:

Id_value_0=23333
Id_value_1=23334
Id_value_2=23335
Id_value_3=22336
Id_value_4=23336

更多信息:

  • Groovy Collection.eachWithIndex()
  • Groovy is the New Black

这篇关于如何使用Groovy逐一读取csv文件数据并将其放入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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