How to append data to csv file in Robot Framework?

Viewed 8692

I would like to append data to .csv file which is not empty in Robot Framework now, but I meet some questions.

I installed CSVLibrary from 's4int' of 'robotframework-CSVLibrary' in github and it has a keyword named 'Append To Csv File'. I can append data into .csv file but there are some issues with the format.

First I have an empty csv file and I run my scripts in Robot Framework.

*** Settings ***
Library           Selenium2Library
Library           CSVLibrary

*** Variables ***

*** Test Cases ***
test
${list}=    Create List    apple    pear
Append To Csv File    ${file_path}    ${list}   

The file is looked like this:

enter image description here

But I expect is:

!enter image description here

How can I append data to show like what I expect? Is my format wrong? Or is there any other way to realize it? Thanks a lot.

2 Answers

If you need to clear the file first and then write another data to it, then you can use below keyword for your robot framework script.

You can directly use the code and create a library with the name CSVLibrary.py and start using it. Then you can call it to your script. Provide the file path with \\ slash to work in the robot framework. eg: E:\\FOLDER1\\FOLDER2. You can give the data in the same way your were giving ${list}=Create List apple pear.

import csv

class CSVLibrary():   

def Clear_file(self, filepath):                    #  Clear_file :it will clear your file. 
 with open(filepath, 'w+') as f:
     obj1=csv.writer(f)

def Append_file(self, filepath, data):             #  Append_file :it will append the data you wanted to in your script. 
  with open(filepath, 'a') as f:
      obj1=csv.writer(f)
      obj1.writerow(data) 
Related