mac
mac

Reputation: 53

RobotFramework :CSV Library : Error while writing into CSV

I am using Robotframework and CSVLibrary to write randomly generated strings. Error while writing into CSV . The following is the code,

${datalist}   CREATE LIST
${list}=        getmandatory              test.xml     testInfo
        : FOR    ${a}    IN  @{list}
                \   ${random}=    String.Generate Random String     ${a.maxlength}      [UPPER]${a.format}
                \   append to list    ${datalist}      ${random}
        log to console   ${datalist}
        append to csv file      data.csv     ${datalist}

The getmandatory is a python pgm wshich returns me a list of all mandatory fields in a given xml. The list values are randomly generated strings , ['BDSVRtZEISBGItUtUMYHBULtUEZQTtDOCBFUGJAPWHXtIeYKTUAWOLSPFBXQCDWLtTIPtOJFTBXSUAYMMNtPRRFMQZGXKBUAtIFD', 'DHeSR']

I am getting an error ,

TypeError: a bytes-like object is required, not 'str'

I am not sure what I am doing wrong here . Please help !

Upvotes: 0

Views: 962

Answers (1)

Eddy Pronk
Eddy Pronk

Reputation: 6705

I could reproduce the issue with CSVLibrary and python3.

In CSVLibrary/init.py you'll find the following function.

@staticmethod
def _open_csv_file_for_write(filename, data, csv_writer=csv.writer, **kwargs):
    with open(filename, 'ab') as csv_handler:
        writer = csv_writer(csv_handler, **kwargs)

Change this to: ('a' instead of 'ab')

@staticmethod
def _open_csv_file_for_write(filename, data, csv_writer=csv.writer, **kwargs):
    with open(filename, 'a') as csv_handler:
        writer = csv_writer(csv_handler, **kwargs)

In Python 3 unicode strings are the default and you can't write them in binary mode.

See TypeError: a bytes-like object is required, not 'str' in python and CSV

Upvotes: 2

Related Questions