Search code examples
pythonpython-3.xfwrite

Python write error - TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'


I have a Python script which prints 3 values x, y, z as follows - 1,2,WELCOME_TO_ROS

Now I want to write these values to a header file in the following format -

#define WELCOME_TO_ROS 1,2, "WELCOME_TO_ROS"

My attempt so far -

f.write('#define %s %d, %d, "%s"') % (z, x, y, z)

What should be the correct format? I am getting the following error -

TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'


Solution

  • Create tuple

    tp = (1,2,"WELCOME_TO_ROS")
    

    Write to header file with file handle f

    f.write('#define {2} {0},{1}, "{2}"'.format(tp[0], tp[1], tp[2]))