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 !

6
  • Please don't post a link to a picture of code. Take the time to copy, paste, and format the code in your question. Commented Apr 15, 2018 at 1:17
  • Sorry. Updated the question. Commented Apr 15, 2018 at 17:15
  • Which line is the error occurring? I usually prefer to write Robot keywords just as high level sentences in English, and leaving fors for Python code Commented Apr 15, 2018 at 19:14
  • Error is happening at “ append to csv file” Commented Apr 16, 2018 at 4:42
  • Are you using python3? Commented Apr 16, 2018 at 12:43

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I did the change as suggested and the data got appended to the csv file. Thank you so much.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.