使用 Robot Framework 读取 CSV 文件并从第 n 列中查找值

CSV file reading and find the value from nth Column using Robot Framework

请帮我找到答案 使用 Robot Framework 读取 CSV 文件并从第 n 列中查找值 我不想使用 excel-library

我有一个包含 5 列和 10000 行的 csv 文件 想使用机器人框架从第 3 列和第 7000 行中找到值,我将如何直接找到该值。

我在下面进行了尝试,我得到了价值,但是循环正在执行第 n 次,这很耗时,并且可能会产生性能问题

${value1}   Create List             
${FILE_CONTENT} Get File    C:\abcd1.csv           
Log File Content: ${FILE_CONTENT}               
@{LINES}    Split to Lines  ${FILE_CONTENT}         
Remove From List    ${LINES}    0           
:FOR    ${LINE} IN  @{LINES}        
     Log    ${LINE}         
     @{ROWS}    Split String    ${LINE} separator=, 
     ${VALUE}   Get From List   ${ROWS} 2   
     Log    ${VALUE}            
     ${value2}  Convert To String   ${VALUE}        
     Run Keyword If     ${value2} == 1400000    Exit For Loop       
     Append To List ${value1}   ${value2}           
     Log    ${value1}

首先,我尝试了我向您建议的内置库

github.com/s4int/robotframework-CSVLibrary

i 运行 遇到了一些错误,这可能是因为我的 data.csv 的格式,但没有足够的时间来调试它。

我在 python 中为您的解决方案创建了一个自定义库,您可以将其用于您的工作

data.csv

Name,Age,region,country,Marks
pankaj,22,delhi,india,45
neeraj,32,noida,india,75

python 使用 csv 模块解析此数据的代码和 return 第 n 行第 n 列的值

import csv
#Previous function to go to nth row and nth column
def go_to_nth_row_nth_column(File,row_no,col_no):
    inputFile = File
    row_no=int(row_no)
    col_no=int(col_no)
    with open(inputFile) as ip:
        reader = csv.reader(ip)
        for i, row in enumerate(reader):
            if i == row_no:      # here's the row 
                #print row[col_no] # here's the column
                return row[col_no]

#Function to find the string values, in case of duplicate occurrence as well
def search_cell(File,search_string):
    inputFile = File
    search_position=[]  #empty list which will later store row,column occurences
    with open(inputFile) as ip:
        reader = csv.reader(ip)
        for i, row in enumerate(reader):
            for j, column in enumerate(row):
                if search_string in column:      # here's the row 
                    #print((i,j))
                    search_position.append((i,j)) #this will create list of list i.e. list of row,columns in case of multi occurences
                    #return (i,j)   
    return search_position

您可以将其用作机器人文件中的库,如下所示

*** Settings ***
Library    csv2.py



 *** Test Cases ***
Test
    Check row column
    Search String

*** Keywords ***
Check row column
    ${result} =    go_to_nth_row_nth_column    data.csv    2    1
    log  ${result}

Search String
    ${result1}=    search_cell    data.csv    india
    log  ${result1}
      ID, Text, Value
      20, Second Item, 200
      30, Third Item, 100
      40, Second Item, 300
      10, First Item, 300
      50, Third Item, 100

      import csv
    def search_cell(string):
    with open("C:\abcd.csv", "rt") as f:
    csvreader = csv.reader(f)
    for index,row in enumerate(csvreader):
        if string in row[2]:
            print "300 spotted"
            return row

      Robot Code
      ${result2}    search_cell 300
     Log    ${result2}  

我得到的答案只有

['40', '第二项', '300'] 我想要两列都有 300

正如@pankaj 所建议的那样,使用库 https://github.com/s4int/robotframework-CSVLibrary 和以下代码。此代码查找变量 ${value_to_find},如果找到 returns,则继续循环并记录消息。如果想在单个列中查找,可以自定义 verify variable 关键字 range/loop。

sample.robot

*** Settings ***
Library                     Collections
Library                     CSVLibrary

*** Variables ***
${value_to_find}    Morris

*** Test Cases ***
mytest
    Read file and verify

*** Keywords ***
Read file and verify
  @{list}=  read csv file to list    data.csv
  :FOR    ${x}    IN    @{list}
  \    Log  ${x}
  \    Set test variable    ${x}
  \    verify variable
  \    Run keyword if    ${status}    Log to console    variable was found!
  \    Exit For Loop If    ${status}
  Run keyword if    not ${status}    Log to console    variable was not found!

verify variable
    ${length}=    Get Length    ${x}
  : FOR    ${j}    IN RANGE    0    ${length}
  \    Log    ${x[${j}]}
  \    ${status}=    Evaluate    '${x[${j}]}' == '${value_to_find}'
  \    Log    ${status}
  \    set test variable    ${status}
  \    Exit For Loop If    ${status}

data.csv

id,first_name,last_name,email,gender,ip_address
1,Douglas,Morris,dmorris0@mozilla.org,Male,205.4.212.229
2,Stephanie,Oliver,soliver1@google.com.br,Female,18.101.154.106
3,Russell,Castillo,rcastillo2@shop-pro.jp,Male,255.52.95.46
4,Helen,Reed,hreed3@rambler.ru,Female,167.55.67.109
5,Jesse,Wagner,jwagner4@histats.com,Male,252.37.62.215
6,Ashley,Diaz,adiaz5@wikia.com,Female,79.87.105.139
7,Rachel,Robinson,rrobinson6@blogger.com,Female,132.66.117.101
8,Phillip,Johnston,pjohnston7@disqus.com,Male,70.152.55.21
9,Craig,Burton,cburton8@toplist.cz,Male,73.117.157.82
10,Patrick,Fisher,pfisher9@1und1.de,Male,2.36.191.97