18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

Writing Data into CSV file

manasa.msmanasa.ms Members
edited November -1 in Sahi - Open Source
Hi
how to write data into CSV file , am making use of _writeCSVFile(array2d, filePath) api , but it is writing each individual character into a cell.

Ex:
part of my code is like this :

var $data=_readCSVFile("/home/trainee/sahi/userdata/scripts/csv_16th/test_csv_new_url.csv");
$keyword=$data[1][0];
_writeCSVFile($data[1][0],"/home/trainee/Desktop/write.csv")

when i run this code it will run successfully but the data (in this case its "url") will be printed in csv file like
u
r
l

Can you help me on how to print that "url" as a single value in a cell of CSV file. Here am saving a openoffice spreadsheet with .csv extension and making use of it as CSV file

Comments

  • TheeranTheeran Members
    Hi manas.ms,

    In order to write some data into CSV file,you should specify the array name and not a cell in array.Since your CSV file is intialized with the particular number of rows and columns,the script will try to divide the input and feed to all the columns.

    Kindly try the below script and check if this solves issue.
    var $data=_readCSVFile("/home/trainee/sahi/userdata/scripts/csv_16th/test_csv_new_url.csv");
    $keyword=$data[1][0];   
    _writeCSVFile($data,"/home/trainee/Desktop/write.csv");
    
    

    Regards
    Theeran
  • SumitraSumitra Members
    Hi manasa.ms,

    Please do not duplicate the post.

    Regards
    Sumitra
  • Hi Theeran,
    Thanks for your reply....
    I tried the modified code , it prints the value("url") in a single cell, but along with that it will print all other data which is there in test_csv_new_url.csv file.
    My scenario is, i want to print the first cell value of "test_csv_new_url.csv" only, in "write.csv" file.

    or

    I want to print the value of a variable Ex: var $keyword = "PASS"
    in this case what should i mention for "array2d" field of _writeCSVFile(array2d, filePath) api.
  • TheeranTheeran Members
    Hi manasa.ms,

    Inorder to get only the first cell value from a CSV file,you should create a new 2darray and store the received values in its first column and pass it as a 2darray parameter by using _writeCSVFile() API.

    Try the following code
    var $data=_readCSVFile("/home/trainee/sahi/userdata/scripts/csv_16th/test_csv_new_url.csv");
    var $value = new Array();
    for(var $i=0;$i<$data1.length;$i++)
    {
        $value[$i] = new Array();
        $value[$i][0] = $data1[$i][0];
    }
    _writeCSVFile($value,"/home/trainee/Desktop/write.csv", true);
    

    Regards
    Theeran
  • Hi Theeran
    The code that you posted will again print all the data of first column, as because, there data of first column(of all rows) is assigned to the newly created array "$value" .

    Can you suggest me what steps can i follow to go with second scenario that is :

    To print the value of a variable Ex: var $keyword = "PASS"
    in this case what should i mention for "array2d" field of _writeCSVFile(array2d, filePath) api.
  • TheeranTheeran Members
    Hai manasa.ms,

    Inorder to get only the first cell value of one CSV file to be printed in another CSV file,try the following code.
    var $data=_readCSVFile("/home/trainee/sahi/userdata/scripts/csv_16th/test_csv_new_url.csv"
    
    var $value = new Array();
    $value[0] = new Array();
    
    $value[0][0] = $data[0][0];
    
    _writeCSVFile($value,"/home/trainee/Desktop/write.csv", true);
    

    and with the case of your second scenario,try the following code,
    var $data=_readCSVFile("/home/trainee/sahi/userdata/scripts/csv_16th/test_csv_new_url.csv");
    var $value = new Array();
    $value[0] = new Array();
    var $keyword = "PASS";
    $value[0][0]=$keyword;
    _writeCSVFile($value,"/home/trainee/Desktop/write.csv", true);
    

    Hope this would help you.

    Regards
    Theeran.
  • Hi Theeran
    Its working, thanks a lot.....
This discussion has been closed.