0

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 !

mac
  • 53
  • 3
  • 12

1 Answers1

2

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

Eddy Pronk
  • 6,527
  • 5
  • 33
  • 57
  • I did the change as suggested and the data got appended to the csv file. Thank you so much. – mac Apr 16 '18 at 13:38
  • The problem right now is, each character in a list value is getting written in to each column. G|O|D| instead of GOD in one single column – mac Apr 16 '18 at 14:01