Search code examples
pythoncsvrobotframeworkrobotframework-ide

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


Please help me to find the answer for CSV file reading and find the value from nth Column using Robot Framework i don't want to use excel-library

I have csv file having 5 columns and 10000 Rows wanted to find value from 3rd column and 7000 Row using robot framework how will i find the value directly.

I tried below and i am getting value but loop is executing for nth time and that is time consuming and may create performance issues

${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}

Solution

  • Firstly i tried with the inbuilt library as suggested by me to you

    github.com/s4int/robotframework-CSVLibrary

    i ran across to some errors,which may be because of format of my data.csv , but did not had enough time to debug that.

    i created a custom library in python for your solution, you can use it for your work

    data.csv

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

    python code to parse this data using csv module and return value of nth row and nth column

    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
    

    you can use this as library in your robot file , like below

    *** 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}