我有一个 Python 变量(数据库记录),打印如下:
((79321L, Decimal('11.56125119')),)
打印这两个值的最佳方式是什么?我想做这样的事情:
print("Count: {}, Result: {}".format(cur.fetchall()))
请您参考如下方法:
In [10]: a=((79321L, Decimal('11.56125119')),)
#a[0] is a tuple, use "*" to unpack a tuple when passing it as parameters:
In [11]: "Count: {}, Result: {}".format(*a[0])
Out[11]: 'Count: 79321, Result: 11.56125119'
参见 how to unpack argument lists , 和 format examples
或使用 old %-formatting operator :
In [13]: "Count: %s, Result: %s"%a[0]
Out[13]: 'Count: 79321, Result: 11.56125119'
