续前文(Flask搭建api服务)把SQL语句以字典的方式存储起来,配置和代码没有分割开来,一直纠结如何处理配置文件中多行配置项的问题,查了半天,其实这个问题不存在的,只要在配置项中跨行部分前面追加一个空格即可。
ini是传统的主流配置文件。
ini支持的数据类型有限,将所有的值都默认成字符串(字符串最外面不需要添加引号)。
ini配置文件必须使用[](section)进行分组,每一个键称为option。
1、追加配置文件SQL.ini
[KPIDB]
sql1 = select a.*
from kpi_value a
where a.kpicode in ('01010101','02010101','03010101')
and a.datelevel='01'
and a.regionlevel='02'
sql2 = select a.*
from kpi_value a
where a.kpicode in ('01010101','02010101','03010101')
and a.datelevel='01'
and a.regionlevel='02'
and a.datecode>=:begindate and a.datecode<=:enddate
sql3 = select a.*
from kpi_value a
and a.datelevel='01'
and a.regionlevel='02'
and a.datecode>=:begindate and a.datecode<=:enddate
and a.kpicode in :kpicode
2、在app.py中追加一个configparser读取
import configparser
config = configparser.ConfigParser()
config.read('SQL.ini',encoding="utf-8-sig")
DBSECTION='KPIDB'
3、在各个路由函数中使用config项替代原来的字典方式
# sqltext = sqldict[sqlid]
sqltext = config[DBSECTION][sqlid]
pycharm读取ini配置文件,使用自带的configparser模块。
具体步骤如下:
1、导入ConfigParser类;
2、实例化ConfigParser;
3、使用read()方法打开并读取文件内容;
4、使用get()方法,根据section和option获取指定的值。
最后,谢谢关注,谢谢支持!
本文分享自 python与大数据分析 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!