【发布时间】:2023-02-07 04:41:59
【问题描述】:
我正在尝试将下面函数的值存储到单个字符串中,我可以将其输入到利用 F 字符串的查询中。输出看起来是正确的,但实际上只是一些单独的打印语句。
如何将下面的输出存储到单个字符串中?
import pandas as pd
view_dict = [{'id':'168058','viewtime_min':'2023-01-26 21:00:59.435 -0600','viewtime_max':'2023-01-26 21:59:59.435 -0600'},
{'id':'167268','viewtime_min':'2023-01-26 21:59:59.435 -0600','viewtime_max':'2023-01-26 21:59:59.435 -0600'},
{'id':'167268','viewtime_min':'2023-01-26 21:59:59.435 -0600','viewtime_max':'2023-01-26 21:59:59.435 -0600'}]
def get_where_clause(view_dictionary: dict):
where_clause = " "
for index in range(len(view_dictionary)):
if index != max(range(len(view_dictionary))):
print(f'''(b.id = {view_dictionary[index]['id']}
and b.viewed_at between coalesce({view_dictionary[index]['viewtime_min']},published_at) and {view_dictionary[index]['viewtime_max']})
or''')
else:
print(f'''(b.id = {view_dictionary[index]['id']}
and b.viewed_at between coalesce({view_dictionary[index]['viewtime_min']},published_at) and {view_dictionary[index]['viewtime_max']})''')
x = get_where_clause(view_dict)
x
我期望这会存储到一个值,但是在访问该值时不会存储任何内容。
【问题讨论】:
-
get_where_clause不返回任何内容,也不更改字典 -
输出如下...我试图将其保存为单个字符串而不是 3 个单独的打印语句。 ```(b.id = 168058 和 b.viewed_at 在合并(2023-01-26 21:00:59.435 -0600,published_at)和 2023-01-26 21:59:59.435 -0600 之间)或(b.id = 167268 和 b.viewed_at 之间 coalesce(2023-01-26 21:59:59.435 -0600,published_at) 和 2023-01-26 21:59:59.435 -0600) or (b.id = 167268 and b.viewed_at between合并(2023-01-26 21:59:59.435 -0600,published_at)和 2023-01-26 21:59:59.435 -0600)```
标签: python python-3.9