python 嵌套字典和 for 循环中的字符串格式

python string formatting within nested dict and for loop

使用以下词典,我正在尝试实现字符串的创建。

query_args= {"columns":{ "investingcom":"datetime, actual, forecast, previous",
                        "bloomberg":"datetime, Actual, `Surv(M)`, Prior",
                        "ft":"datetime, actual, forecast, prior"},
             "table":{"investingcom":"`all-data`.`indicators`",
                     "bloomberg":"`all-data`.`indicators-bloomberg`",
                      "ft":"`all-data`.indicators_ft"},
             "country":{"investingcom":"\"US\"",
                     "bloomberg":"\"US\"",
                      "ft":"\"United States\""},
             "name":{"investingcom":"\"Average Hourly Earnings (MoM)\"",
                     "bloomberg":"\"Average Hourly Earnings MoM\"",
                      "ft":"\"Average Hourly Earnings %% m/m\""}
                     }

我需要创建 3 个字符串,因此使用 for 循环:

for ind_source in ["investingcom", "bloomberg", "ft"]:
    print('SELECT {columns[ind_source]} FROM {table[ind_source]} \
    WHERE country={country[ind_source]} AND name={name[ind_source]}'.format(**query_args))

然而这returns一个错误:

KeyError: 'ind_source'

关于如何使该实施成功的任何建议?

怎么样:

for ind_source in ["investingcom", "bloomberg", "ft"]:
    print(('SELECT {columns[' +ind_source + ']} FROM {table[' + ind_source + ']} \
    WHERE country={country[' + ind_source + ']} AND name={name[' + ind_source + ']}').format(**query_args))

如果将 'ind_source' 放入字符串中,格式化时它将在 query_args 字典中搜索关键字 'ind_source'。这本字典没有那个键,因此没有那个错误。 而是在 for 循环中将字符串与 ind_source 的当前值连接起来。

我不熟悉你在你的例子中尝试的方式(尽管通过一些调整可能会这样做。但是,我会这样做的方式如下所示:

query_args= {"columns":{ "investingcom":"datetime, actual, forecast, previous",
                        "bloomberg":"datetime, Actual, `Surv(M)`, Prior",
                        "ft":"datetime, actual, forecast, prior"},
             "table":{"investingcom":"`all-data`.`indicators`",
                     "bloomberg":"`all-data`.`indicators-bloomberg`",
                      "ft":"`all-data`.indicators_ft"},
             "country":{"investingcom":"\"US\"",
                     "bloomberg":"\"US\"",
                      "ft":"\"United States\""},
             "name":{"investingcom":"\"Average Hourly Earnings (MoM)\"",
                     "bloomberg":"\"Average Hourly Earnings MoM\"",
                      "ft":"\"Average Hourly Earnings %% m/m\""}
                     }

for ind_source in ["investingcom", "bloomberg", "ft"]:
    print('SELECT {} FROM {} \
    WHERE country={} AND name={}'.format(
        query_args['columns'][ind_source],
        query_args['table'][ind_source],
        query_args['country'][ind_source],
        query_args['name'][ind_source],
    ))

我会尝试以下方法:

for ind_source in ["investingcom", "bloomberg", "ft"]:
     print(
         ("SELECT {columns[%(ind_source)s]} FROM {table[%(ind_source)s]}"
          "WHERE country={country[%(ind_source)s]} AND "
          "name={name[%(ind_source)s]}" % { 'ind_source': ind_source })
     .format(**query_args))

本质上是连续使用两个 format 操作来获得您想要的查询。

与其尝试执行复杂的格式,不如先尝试过滤数据以获得更清晰的格式字符串和可读性。

query_args= {"columns":{ "investingcom":"datetime, actual, forecast, previous",
                        "bloomberg":"datetime, Actual, `Surv(M)`, Prior",
                        "ft":"datetime, actual, forecast, prior"},
             "table":{"investingcom":"`all-data`.`indicators`",
                     "bloomberg":"`all-data`.`indicators-bloomberg`",
                      "ft":"`all-data`.indicators_ft"},
             "country":{"investingcom":"\"US\"",
                     "bloomberg":"\"US\"",
                      "ft":"\"United States\""},
             "name":{"investingcom":"\"Average Hourly Earnings (MoM)\"",
                     "bloomberg":"\"Average Hourly Earnings MoM\"",
                      "ft":"\"Average Hourly Earnings %% m/m\""}
                     }

for ind_source in ["investingcom", "bloomberg", "ft"]:
    args = {x: query_args[x][ind_source] for x in query_args}
    print('SELECT {columns} FROM {table} \
    WHERE country={country} AND name={name}'.format(**args))