【发布时间】:2021-02-24 19:04:52
【问题描述】:
我有一个如下所示的 sql_data.py 文件:
TABLE_A = '''Select * from Table_A;'''
TABLE_B = '''Select * from Table_B;'''
我有一个 main.py 文件,我想在其中循环这些变量并打印 SQL。
import sql_data
tables = ['TABLE_A', 'TABLE_B']
# this doesn't work, but this is what I am looking for. Something dynamic like below
for table in tables:
print(SQL_DATA.[table])
# this works, but it's hard coding in the table name which I don't want
for table in tables:
print(SQL_DATA.TABLE_A)
有什么建议吗?
【问题讨论】:
-
你试过
SQL_DATA.[table]吗? -
是的,对不起。代码中有错字。我确实尝试过小写。没用。