You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Robot Framework单条件For循环及CSV文件解析技术问询

Hey there! Let's break down your two Robot Framework questions with clear, actionable solutions:

1. Running a For Loop with a Single Condition

Robot Framework's : FOR loop is built to iterate over a sequence, so to run it against a single condition (like executing the loop body once, or only when a specific check passes), you have two straightforward approaches:

Option 1: Use a single-element list

If you just need to run the loop once, create a list with one item and iterate over it:

@{single_run}    Create List    dummy_value
: FOR    ${item}    IN    @{single_run}
\    Log    Executing loop body once
\    # Add your keyword logic here (e.g., Validate Single Condition)

Option 2: Control loop count with a condition

If you want the loop to run only when a condition is true, use IN RANGE with a dynamic count (1 if the condition passes, 0 otherwise):

${is_condition_met}    Set Variable    ${True}  # Replace with your actual condition check
: FOR    ${dummy}    IN RANGE    ${1 if is_condition_met else 0}
\    Log    Running loop because condition is satisfied
\    # Your conditional logic goes here
2. Parsing the session_data.csv File

Looking at your CSV structure, there's a header row and some entries with missing EndTime (like Case_2). Your current code is on the right track, but we need to handle headers, whitespace, and missing fields. Here's an improved version:

First, let's recap your CSV content for clarity:

Case, StartTime, EndTime
Case_1=Covering 3 time-slots,T00:00:00,T05:00:00
Case_2=Covering multiple time-slots,T00:15:00
Case_3=Covering one time-slot,T00:18:00,T00:47:00

Improved Parsing Code

*** Settings ***
Library    String
Library    Collections

*** Test Cases ***
Parse Session Data
    # Read the CSV file into a list of lines
    ${raw_lines}=    Process Data File    ${CURDIR}/session_data.csv
    # Skip the header row (first line) to get only data rows
    ${data_rows}=    Get Slice From List    ${raw_lines}    1    -1

    : FOR    ${line}    IN    @{data_rows}
    \    Log    Processing line: ${line}
    \    # Split line by commas, then clean up whitespace from each field
    \    @{raw_columns}=    Split String    ${line}    separator=,
    \    @{clean_columns}=    Create List
    \    : FOR    ${col}    IN    @{raw_columns}
    \    \    ${clean_col}=    Strip String    ${col}
    \    \    Append To List    ${clean_columns}    ${clean_col}
    \    # Extract fields with fallback for missing EndTime
    \    ${test_case}=    Get From List    ${clean_columns}    0
    \    ${start_time}=    Get From List    ${clean_columns}    1
    \    # Check if EndTime exists; use "N/A" if missing
    \    ${end_time}=    Run Keyword If    ${len(clean_columns)} < 3    Set Variable    N/A
    \    ...    ELSE    Get From List    ${clean_columns}    2
    \    Log    Case: ${test_case} | Start: ${start_time} | End: ${end_time}

Key Improvements:

  • Skip headers: We slice the raw lines list to exclude the first header row, so we only process actual data entries.
  • Clean whitespace: Using Strip String removes extra spaces around fields (like the space after commas in the header line).
  • Handle missing fields: We check the length of the cleaned columns list to detect missing EndTime, then assign a default value ("N/A") instead of throwing an index error.

内容的提问来源于stack exchange,提问作者S.C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:15:07