【发布时间】:2022-10-02 13:42:26
【问题描述】:
我有许多列表,如下所示,通过打印data 并将其与字符串check_str 连接起来,如下最后一条语句所示。如预期所示,在字符串连接后需要帮助更好地格式化列表值
print(data) #printing lists in this variable
[\'0 rows returned\']
[(Decimal(\'1456\'),datetime.date(2022, 9, 15)), (Decimal(\'156\'),datetime.date(2012, 10, 15))]
[\'0 rows returned\']
[\'0 rows returned\']
[(Decimal(\'1256\'),datetime.date(2112, 9, 15)), (Decimal(\'56\'),datetime.date(2012, 10, 25)), (Decimal(\'561\'),datetime.date(2012, 10, 25))]
将每个列表与字符串 check_str 连接起来
check_str = \'data is there\'
#Below is the last statement where I am concatenating string with each list from data variable
print(check_str + \'|\' + \',\'.join(str(v) for v in data))
上述打印语句的实际输出:
data is there|0 rows returned
data is there|(Decimal(\'1456\'),datetime.date(2022, 9, 15))(Decimal(\'156\'),datetime.date(2012, 10, 15))
data is there|0 rows returned
data is there|0 rows returned
data is there|(Decimal(\'1256\'),datetime.date(2112, 9, 15)), (Decimal(\'56\'),datetime.date(2012, 10, 25)), (Decimal(\'561\'),datetime.date(2012, 10, 25))
预期:如果可能,需要以以下良好格式提供以上输出。
data is there|0 rows returned
data is there|1456|2022-09-15
|156|2022-10-15
data is there|0 rows returned
data is there|0 rows returned
data is there|1256|2112-09-15
|56|2012-10-25
|561|2012-10-25
-
正确解决问题需要理解您实际拥有的数据是什么,然后想出一个明确的规范对于应该发生的事情。
str是锤子;你没有钉子,甚至没有螺丝,只有精美的瓷茶杯。要解决问题,首先要将其分解为合乎逻辑的步骤。例如,取一个元素,列表之一在导致问题的data中,决定结果应该是什么格式化该部分,并编写处理它的代码。然后编写代码来检测和解决这些情况。然后编写代码将该逻辑应用于所有内容。 -
我投票结束这个问题,因为“需要更多关注”。在发布问题之前进行此类分析是您的责任。请阅读How to Ask。
标签: python python-3.x list format