How to loop/read from csv file and write to new csv per line

I found the !csvLine unclear as to how you write to the next line within your saved csv. I realized that each save started a new line. I also couldn’t find an example of looping through a csv, doing something with that variable, then writing to a new csv based on that line.

This script will go through each line in ‘currency-names.csv’, find the USD converted rate via xpath, save that value to a new csv called csvreadwritetest. currency-names.csv is a single column with values:
EUR
CNY
INR
BTC

{
  "Name": "DemoCsv_Loop_Read_Write_Save",
  "CreationDate": "2019-6-24",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed"
    },
    {
      "Command": "open",
      "Target": "https://a9t9.com/kantu/demo/csvsave",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "currency-names.csv",
      "Value": "csvFile"
    },
    {
      "Command": "csvRead",
      "Target": "${csvFile}",
      "Value": "csvFile"
    },
    {
      "Command": "echo",
      "Target": "Status = ${!csvReadStatus}, line = ${!csvReadLineNumber}",
      "Value": ""
    },
    {
      "Command": "while",
      "Target": "\"${!csvReadStatus}\" == \"OK\"",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "${!COL1}",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "${!COL1}",
      "Value": "!csvLine"
    },
    {
      "Command": "storeText",
      "Target": "//tbody/tr/th[2]/a[text()='${!COL1}']/following::a[text()][1]",
      "Value": "c2"
    },
    {
      "Command": "store",
      "Target": "${c2}",
      "Value": "!csvLine"
    },
    {
      "Command": "echo",
      "Target": "Currency ${!COL1} is ${c2} when converted to USD.",
      "Value": "!csvLine"
    },
    {
      "Command": "store",
      "Target": "Currency ${!COL1} is ${c2} when converted to USD.",
      "Value": "!csvLine"
    },
    {
      "Command": "csvSave",
      "Target": "csvReadWriteTest",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "${!csvReadLineNumber}+1",
      "Value": "!csvReadLineNumber"
    },
    {
      "Command": "store",
      "Target": "true",
      "Value": "!errorIgnore"
    },
    {
      "Command": "echo",
      "Target": "Reading CSV line No.  ${!csvReadLineNumber} ",
      "Value": "!errorIgnore"
    },
    {
      "Command": "echo",
      "Target": "Reading CSV line No.  ${!csvReadLineNumber} ",
      "Value": "!errorIgnore"
    },
    {
      "Command": "csvRead",
      "Target": "${csvFile}",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "false",
      "Value": "!errorIgnore"
    },
    {
      "Command": "endWhile",
      "Target": "",
      "Value": ""
    }
  ]
}

… !csvLine unclear as to how you write to the next line within your saved csv.

You do this with csvSave. This command saves the current values inside !csvLine to the CSV file and then resets !csvLine to “empty”, ready to add the next line

I realized that each save started a new line.

Yes!

I also couldn’t find an example of looping through a csv, doing something with that variable, then writing to a new csv based on that line.

Have you seen these demo files already?

demo1

1 Like