python-3.6 - 如何将预定义的变量值传递到 Python 中预定义的硬编码字符串的一部分?
问题描述
我有下面的硬编码字符串,我从 MySql 中的配置表中读取。(并且我们的数据库中有许多硬编码 {},我不能去 mysql 识别所有变量并在脚本中手动处理它。)
string_from_sql_table = '/distribution/{partner}/{process_date}/'
我的 python 脚本中有以下预定义变量。
from datetime import datetime
string_from_sql_table = '/distribution/{partner}/{process_date}/' #to keep simple, didn't include code to read the string from mysql table.
partner = 'startupco'
process_date = datetime.today().strftime('%Y%m%d_%H%M')
我打印时得到以下输出(string_from_sql_table):
/distribution/{partner}/{process_date}/
如何在不更改输入 string_from_sql_table 或更改输入 string_from_sql_table 的情况下低于预期输出:
/distribution/startupco/20191011_1317/
解决方案
简单的
'/distribution/{partner}/{process_date}/'.format(
partner = 'startupco',
process_date = datetime.today().strftime('%Y%m%d_%H%M')
)
在此处阅读有关该主题的更多信息https://docs.python.org/3/library/stdtypes.html#str.format
推荐阅读
- sql - 如何在oracle Sql中获取满足第一行值为N的条件的记录,然后进一步获取其值为Y的记录
- c# - 如何引用 Linux 容器上的 DLL 文件?
- visual-studio-code - 在 Visual Code Studio 中选择每一行的 2sd 指定字母
- javascript - Node.js 未安装在 Windows 10 上
- sql - 添加列以计算主值下的子值数量
- reactjs - 切换按钮时出现“TypeError:visible.map 不是函数”
- android - 如何将 TestCoroutineScope 与 android 数据存储区一起使用?
- javafx - JavaFX Spinner 在警报后继续旋转
- sql - 如何使用 postgresql 计算不同组之间的相关性
- javascript - Laravel-Mix:在 Laravel-Mix 升级到 v6 后,Vue 在 app.js 和 bootstrap.js 中不可用