Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 机器人框架:与FOR循环相连_Python 3.x_Python 2.7_Automated Tests_Robotframework - Fatal编程技术网

Python 3.x 机器人框架:与FOR循环相连

Python 3.x 机器人框架:与FOR循环相连,python-3.x,python-2.7,automated-tests,robotframework,Python 3.x,Python 2.7,Automated Tests,Robotframework,我对使用RobotFramework和尝试使用Catenate格式化字符串相当陌生,不知道在格式化字符串时是否可以使用FOR循环,下面的格式是否正确?任何建议都将不胜感激,谢谢 ${data}= Catenate ... { ... "email_address": "${NewUserEmailID}", ... "user_name": "${UserName}", ... "roles": [ ... :FOR ${roleId}, ${catID

我对使用RobotFramework和尝试使用Catenate格式化字符串相当陌生,不知道在格式化字符串时是否可以使用FOR循环,下面的格式是否正确?任何建议都将不胜感激,谢谢

${data}=    Catenate
...    {
...    "email_address": "${NewUserEmailID}",
...    "user_name": "${UserName}",
...    "roles": [
...    :FOR    ${roleId}, ${catID}, ${subcatID}    IN    @{role_id_list}, @{category_id_list}, @{subCat_id_list}
...    /    {
...    /    "role_id": ${roleId},
...    /    "categories": [{ "category_id": "${catID}" }],
...    /    "subcats":[{ "sub_category_id": "${subcatID}" }]
...    /    },
...    ],
...    "line_manager": "${LineManageID}",
...    "guest_user": ${GuestUser},
...    "guest_invitation_text":    "Invitation Text",
...    "guest_redirect_url":    "http://www.example.com/guest",
...    "organization_id":    1
...    }
在格式化字符串时是否可以使用FOR循环

否,在循环被执行的意义上,并将每个迭代的值附加到
Catenate
目标。将发生的情况是,您将在结束字符串中包含“:FOR“,“@{role\u id\u list}”等

但是实现您想要的并不是那么难——只要有一个循环来构造中间字符串,并将其附加到
Catenate
中即可。像这样:

${roles array}=    Set Variable    ${EMPTY}    # initialize an empty string variable

:FOR    ${roleId}    ${catID}    ${subcatID}    IN ZIP    ${role_id_list}    ${category_id_list}    ${subCat_id_list}
/    ${roles array}=    Set Variable    ${roles array}{  # you append the target string to the end of the current value of the variable
/    ${roles array}=    Set Variable    ${roles array}"role_id": ${roleId},
/    ${roles array}=    Set Variable    ${roles array}"categories": [{ "category_id": "${catID}" }],
/    ${roles array}=    Set Variable    ${roles array}"subcats":[{ "sub_category_id": "${subcatID}" }]
/    ${roles array}=    Set Variable    ${roles array}},

# and now use it inside the Catenate
${data}=    Catenate
...    {
...    "email_address": "${NewUserEmailID}",
...    "user_name": "${UserName}",
...    "roles": [ ${roles array}
...    ],
...    "line_manager": "${LineManageID}",
...    "guest_user": ${GuestUser},
...    "guest_invitation_text":    "Invitation Text",
...    "guest_redirect_url":    "http://www.example.com/guest",
...    "organization_id":    1
...    }

正如您所注意到的,迭代多个列表的语法有点不同-需要使用(来自/类似于python的
zip()
函数)。

您试过运行它吗?;)不,至少不会以您希望的方式运行-循环不会执行,您只会得到包含“:FOR“,“@{role_id_list}”等的结束字符串。您应该在纯python中为此实现一个特殊的catenate关键字。