Skip to main content
 首页 » 编程设计

python之RF : Replace ${SPACE}, ${EMPTY} 和之值为 '0'

2025年05月04日14itcoder

目前正在使用 Robot Framework List。 我有一个像这样的列表 ['Thu 2/22/2018', ' ', '-', '0.0000', '0', '', 'Fri 2/23/2018', '0', '-', '0.0000', '0', '+20 hr 28 min', 'Sat 2/24/2018', '37', '37', '0.0000', '37', '-10 hr 23 min'] 我想将 ${SPACE}、${EMPTY} 和 - 值转换为“0”。我正在尝试使用以下代码,但出现了这些错误

ValueError: Cannot convert index ' ' to an integer. 
ValueError: Cannot convert index '-' to an integer. 
ValueError: Cannot convert index '' to an integer. 

这是代码

:FOR   ${x}  IN   @{OnlyList} 
\  Log   ${x}   console=true 
\   run keyword if  '${x}'== '${SPACE}'  Collections.set list value  ${OnlyList}  ${x}  0 
\   run keyword if  '${x}'== '-'  Collections.set list value  ${OnlyList}  ${x}  0 
\   run keyword if  '${x}'== '${EMPTY}'  Collections.set list value  ${OnlyList}  ${x}  0 

不确定如何转换。请帮助。

请您参考如下方法:

Set List Value 将 index 作为第二个参数。

${i}    Set Variable    ${0} 
:FOR   ${x}  IN   @{OnlyList} 
\  Log   ${x}   console=true 
\   run keyword if  '${x}'== '${SPACE}'  Collections.set list value  ${OnlyList}  ${i}  0 
\   run keyword if  '${x}'== '-'  Collections.set list value  ${OnlyList}  ${i}  0 
\   run keyword if  '${x}'== '${EMPTY}'  Collections.set list value  ${OnlyList}  ${i}  0 
\   ${i}    Set Variable    ${i+1} 
Log   ${OnlyList}   console=true