隨着下面的字典,我試圖實現字符串的創建。嵌套字典和循環內的python字符串格式
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 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))
然而,這會返回一個錯誤:
KeyError: 'ind_source'
任何建議如何使執行成功?
感謝您的回覆。但是,現在我收到以下錯誤:'單個'}'遇到格式字符串' –
您仍然需要修復最後一個鍵(名稱) –
您是對的,已編輯! –