【发布时间】:2021-02-01 22:55:37
【问题描述】:
代码:
schema = [{"name": "fb_table_1", "dimension": "department", "metrics": ["headcount"]},
{"name": "fb_table_2", "dimension": "area", "metrics": ["sale"]},
{"name": "fb_table_3", "dimension": "product", "metrics": ["quantity", "revenue"]}]
template = f"select date, '{table["dimension"]}', {table['dimension']}, '{metric}', {metric} from {table['name']}"
sql_2 = " union ".join([template for table in schema for metric in table["metrics"]])
print(sql_2)
得到以下错误:
template = f"select date, '{table["dimension"]}', {table['dimension']}, '{metric}', {metric} from {table['name']}"
^
SyntaxError: invalid syntax
是嵌套的单双引号引起的吗?
更新:当我更改为
template = f"select date, '{table['dimension']}', {table['dimension']}, '{metric}', {metric} from {table['name']}"
我收到另一个错误:
NameError: name 'table' is not defined
这是什么原因?
【问题讨论】:
-
是的,你只需要转义
f"select date, \"{table['dimension']}\"...
标签: python python-3.x string