Search code examples
pythonpython-3.xzapier

Zapier Code: Python, generating a CSV string


I am trying to generate a simple csv string (not file!) with Zapier Code (Python). The given code is working when I'm typing it in the Python REPL on my computer, but Zapier is telling me...

'str' object has no attribute 'copy'

which doesn't make any sense to me...

Anyone had this problem too and can give me a solution?

I Already printed out the python version, just to make sure this isn't the problem, it is Version 3.7.

import io
import csv
# root = ElementTree.fromstring(input_data['xml']);
stringio = io.StringIO()
fieldnames = ['first_name', 'last_name']
writer = csv.DictWriter(stringio, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
return stringio.getvalue()

Solution

  • Python Code by Zapier expects a dict output, you're returning a str (the error message could be better about this).

    In any case, amend your code to the following:

    # ...
    return {'result': stringio.getvalue()}
    

    and that should work.