【问题标题】:Python write error - TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'Python 写入错误 - TypeError: %: 'tuple' 和 'tuple' 不支持的操作数类型
【发布时间】:2018-04-13 01:25:54
【问题描述】:

我有一个 Python 脚本,它打印 3 个值 x, y, z,如下所示 - 1,2,WELCOME_TO_ROS

现在我想将这些值写入以下格式的头文件 -

#define WELCOME_TO_ROS 1,2, "WELCOME_TO_ROS"

到目前为止我的尝试 -

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

正确的格式应该是什么?我收到以下错误 -

TypeError: %: 'tuple' 和 'tuple' 的操作数类型不受支持

【问题讨论】:

  • 把所有格式化% tuple放在字符串旁边的括号内
  • 是的@PRMoureu 是对的

标签: python python-3.x fwrite


【解决方案1】:

创建元组

tp = (1,2,"WELCOME_TO_ROS")

使用文件句柄 f 写入头文件

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

【讨论】:

  • 完美!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-05
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
相关资源
最近更新 更多