前言
我们经常会遇到我们对数据库中数据进行拉取并处理脚本,我们如果要操作线上数据库。用java操作就过于麻烦,这个时候我们更方便的方法是直接通过一个python脚本去读取数据库,并进行处理数据。那么我们可以将读取数据库数据,写入txt文件,excel文件,读取txt文件,excel文件作为一个标板。用于后续直接修改文件内容
实践
我们在项目中创建如下文件夹文件

conf-dev.ini 配置文件
[db-config]
host = localhost
port = 3306
username = root
password = 123456
database = mybatis
[file-config]
file_name=net_wall_dev.xlsx
我们通过创建ReadConfig.py环境配置读取配置文件内容
# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# Name: ReadConfig
# Description:
# Author: Administrator
# Date: 2022/1/15
#-------------------------------------------------------------------------------
import configparser
import os
class ReadConfig:
def __init__(self,env=None):
if env:
env = env
else:
env= "dev"
print("env="+env)
#root_dir=os.path.abspath(".") #当前目录的绝对路径
#root_dir=os.path.abspath(r"..") #上级目录的绝对路径
root_dir=os.path.dirname(os.path.abspath(__file__))
config_name="config-{}.ini".format(env)
config_path= os.path.join(root_dir,config_name)
print("config_path="+config_path)
self.cf = configparser.ConfigParser()
self.cf.read(config_path)
def get_db_config(self,param):
value=self.cf.get("db-config",param)
return value
def get_file_config(self,param):
value=self.cf.get("file-config",param)
return value
if __name__ == '__main__':
test= ReadConfig(

最低0.47元/天 解锁文章
1590

被折叠的 条评论
为什么被折叠?



