How to random select csv value

Hi all,

I import a test.csv file

Example:

www,sssr,eded,fdff
123,232,324
aaa,bbb,ccc,ddd,eeeed,bgd
aas,bbd,cca

I want to random select type from Line1,id=A1 Math.floor(Math.random()*4
then random select type from Line2 id=A2 Math.floor(Math.random()*3
then random select type from Line3 id=A3 Math.floor(Math.random()*6
then random select type from Line4 id=A4 Math.floor(Math.random()*3

How to solve?
pls help,thanks!

  1. Generate a random value using storeEval

  2. Assign the value to !csvReadLineNumber

    storeEval | Math.floor(Math.random()*11) | myRandomValue
    store | ${myRandomValue} | !csvReadLineNumber
    csvRead | test.csv

=> this reads a random row from your CSV file.

See also How to resume kantu from where it left off if it lost connection with db? - #2 by admin - UI.Vision RPA - UI.Vision RPA Software Forum | Discuss RPA Automation, Selenium IDE and OCR API Text Recognition (the sample macro in this post uses !csvReadLineNumber, too)

1 Like

If you also want to select the column randomly, it gets a bit more complicated, but a solution to access ${!COL1}, {!COL2 },… based on an index can be found here: Using excel value with loop - not printing values - printing col numbers - #2 by admin - UI.Vision RPA - UI.Vision RPA Software Forum | Discuss RPA Automation, Selenium IDE and OCR API Text Recognition

Thanks,very useful
I solve it like this.

change the csv row to column

www,sssr,eded,fdff

123,232,324

aaa,bbb,ccc,ddd,eeeed,bgd

aas,bbd,cca

to

www,123,aaa,aas
sssr,232,bbb,bbd,
eded,324,ccc,cca
fdff,0,ddd,0
0,0,eeeed,0
0,0,dgd,0

{
  "CreationDate": "2018-9-19",
  "Commands": [
    {
      "Command": "storeEval",
      "Target": "Math.floor(Math.random()*4)+1",
      "Value": "myRandomValue"
    },
    {
      "Command": "store",
      "Target": "${myRandomValue}",
      "Value": "!csvReadLineNumber"
    },
    {
      "Command": "csvRead",
      "Target": "test.csv",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "id=A1",
      "Value": "${!COL1}"
    },
    {
      "Command": "storeEval",
      "Target": "Math.floor(Math.random()*3)+1",
      "Value": "myRandomValue"
    },
    {
      "Command": "store",
      "Target": "${myRandomValue}",
      "Value": "!csvReadLineNumber"
    },
    {
      "Command": "csvRead",
      "Target": "test.csv",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "id=A2",
      "Value": "${!COL2}"
    },
    {
      "Command": "storeEval",
      "Target": "Math.floor(Math.random()*6)+1",
      "Value": "myRandomValue"
    },
    {
      "Command": "store",
      "Target": "${myRandomValue}",
      "Value": "!csvReadLineNumber"
    },
    {
      "Command": "csvRead",
      "Target": "test.csv",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "id=A3",
      "Value": "${!COL3}"
    },
    {
      "Command": "storeEval",
      "Target": "Math.floor(Math.random()*3)+1",
      "Value": "myRandomValue"
    },
    {
      "Command": "store",
      "Target": "${myRandomValue}",
      "Value": "!csvReadLineNumber"
    },
    {
      "Command": "csvRead",
      "Target": "test.csv",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "id=A4",
      "Value": "${!COL4}"
    }
  ]
}

run well

Hi.
Is it possible to save in a random or specific row or even column from a csv?

This post is about reading from a CSV. If you have a question about saving values, I recommend that you open a new thread.

1 Like