Em Sales
Em Sales

Reputation: 3

How to convert an empty string to 0 in robot framework?

Im trying to replace an empty string to 0 but im having an error that the ${current_value} is still an empty string.
Error: Evaluating expression '0 + ' failed: SyntaxError: unexpected EOF while parsing

${maxRows}  Get Row Count  ${FEEDBACK_TRANSACTION_SHEETNAME}
${TOTAL_USD}=    Set Variable    ${0}

FOR    ${i1}    IN RANGE    11  ${maxRows}
     ${current_value}  Read Cell Data By Coordinates  ${FEEDBACK_TRANSACTION_SHEETNAME}  5  ${i1}
     ${current_value} =  Convert To String    ${current_value}
     Run keyword if  '${current_value}' == '${SPACE}'  Replace String  ${current_value}  ${empty}  0
     ...  ELSE IF  '${current_value}' == '-'  Replace String  ${current_value}  ${empty}  0
     ...  ELSE IF  '${current_value}' == '${EMPTY}'  Replace String  ${current_value}  ${empty}  0
     ...  ELSE  Log  not empty
     Log  ${current_value}
     Exit For Loop If  ${i1} == ${maxRows} - 4
     ${TOTAL_USD}=  Evaluate  ${TOTAL_USD} + ${current_value}
END
Set Suite Variable  ${TOTAL_USD}

Upvotes: 0

Views: 1200

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

How to convert an empty string to 0 in robot framework?

You can use the python idiom x = x or y with the Evaluate keyword, which will preserve x if it is truthy, or set it to y otherwise. You also need to use the special variable syntax without the curly braces so that robot uses the actual variable in the expression rather than the string representation of the variable.

${current_value}=  evaluate  $current_value or 0

Here is a complete test:

*** Variables ***
${current_value}  ${EMPTY}

*** Test cases ***
Example
    ${current_value}=  evaluate  $current_value or 0
    should be equal as numbers  ${current_value}  0

Upvotes: 1

Related Questions