I'm trying read 3 or more column from CSV file, it giving me an index error. anyone have an idea

一笑奈何 提交于 2020-01-06 20:24:40

问题


//Create a new filereader object, using the context variable so it can be used between test components
context.fileReader = new BufferedReader(new FileReader('C:/data.csv'))
//Read in the first line of the data file
//this is the code fro the testcase 

firstLine = context.fileReader.readLine()
//Split the first line into a string array and assign the array elements to various test case properties
String[] propData = firstLine.split(",")
testCase.setPropertyValue("data1",propData[0])
testCase.setPropertyValue("data2",propData[1])
testCase.setPropertyValue("data3",propData[2])

//Rename request test steps for readability in the log; append the element name to the test step names
testCase.getTestStepAt(0).setName("data1-" + propData[0])
testCase.getTestStepAt(1).setName("data2-" + propData[1])
testCase.getTestStepAt(2).setName("data3-" + propData[2])

//this is the Code that reads from CSV file
context.fileReader = new BufferedReader(new FileReader('C:/data.csv'))
/*Read in the next line of the file
  We can use the same fileReader created in the Setup script because it
  was assigned to the context variable.*/
nextLine = context.fileReader.readLine()
/*If the end of the file hasn't been reached (nextLine does NOT equal null)
  split the line and assign new property values, rename test request steps,
  and go back to the first test request step*/
if(nextLine != null){
    String[] propData = nextLine.split(",")
    curTC = testRunner.testCase
    curTC.setPropertyValue("data1",propData[0])
    curTC.setPropertyValue("data2",propData[1])
    curTC.setPropertyValue("data3",propData[2])

    curTC.getTestStepAt(0).setName("data1-" + propData[0])
    curTC.getTestStepAt(1).setName("data2-" + propData[1])
    curTC.getTestStepAt(2).setName("data3-" + propData[2])

    testRunner.gotoStep(0)
}  

This is the error that I'm getting. Does anyone have any idea? I'm trying to read more than 3 columns from the CSV file, please help.

TestCase failed [java.lang.IndexOutOfBoundsException: Index: 2, Size: 2:java.lang.IndexOutOfBoundsException: Index: 2, Size: 2], time taken = 0

Here is CSV file data:

Hydrogen,1,H,1.00797,20.4
Carbon,6,C,12.0115,5100
Oxygen,8,O,15.9994,90.2
Gold,79,Gd,196.967,3239
Uranium,92,U,238.03,4091

回答1:


Use OpenCSV instead for parsing CSV files with Java or Groovy. You can add the jar to the Groovy classpath (and dynamically resolve their dependencies) by using Grapes like this:

@Grab(group='com.opencsv', module='opencsv', version='3.3')




回答2:


You're in luck. It's no problem that you're new to SoapUI, because OpenCSV doesn't have anything to do with SoapUI :)

How to read a CSV file using OpenCSV and Groovy

@Grab('com.opencsv:opencsv:3.5')

import com.opencsv.CSVReader

/*
 * Mock some CSV data
 */
def reader = new StringReader(
'''column1,column2,column3,column4,column5
Hydrogen,1,H,1.00797,20.4
Carbon,6,C,12.0115,5100
Oxygen,8,O,15.9994,90.2
Gold,79,Gd,196.967,3239
Uranium,92,U,238.03,4091''')

/*
 * A nice mapping to give each field in the CSV file a name.
 * Much better than a bunch of propData[n] all over the place.
 */
def field = [
    ELEMENT: 0
]

reader.withReader {
    new CSVReader(it).eachWithIndex {list, index ->
        if(index == 0) {
            /*
             * Do whatever you need to do with the header of the CSV file.
             * Example:
             * testCase.setPropertyValue("data1",list[field.ELEMENT])         
             */         
        } else {
            /*
             * Do whatever you need to do with the remaining rows.
             * Example:
             * curTC.setPropertyValue("data1",list[field.ELEMENT])
             */
        }
    }
}

Header and Data

You'll notice that in the eachWithIndex() loop there's an if-else. This makes it possible to process the header and then proceed with the remaining rows without having to restart reading the file.

You should be able to plug your SoapUI-specific code into the appropriate section.

Varying number of fields

If for some reason your data rows don't all have the same number of fields, you can check how many fields there are like this: list.size()



来源:https://stackoverflow.com/questions/30463403/im-trying-read-3-or-more-column-from-csv-file-it-giving-me-an-index-error-any

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!