Python读取cfg文件

    

[mysql]
HOST = 127.0.0.1
PORT = 3306
USER = root
PWD = 123456789
DB = employees
CHARSET = utf8


[redis]
# #redis配置,暂时写在这里,线下配置。线上一定要从新配置,并且不要上传到gitlab。 非常重要!!!
HOST = 127.0.0.1
PORT = 6379
PWD =
DBID = 0


import pymysql
import configparser
cp = configparser.ConfigParser()
cp.read('test.cfg')
conn = pymysql.connect(
        host = cp.get('mysql', 'HOST'),
        user = cp.get('mysql', 'USER'),
        passwd = cp.get('mysql', 'PWD'),
        db = cp.get('mysql', 'DB'),
        port = cp.getint('mysql', 'PORT'),
        charset= cp.get("mysql", 'CHARSET'),
    )
cur = conn.cursor()
sql="""select * from mushroom"""
cur.execute(sql)
data=cur.fetchall()
print(data)



你可能感兴趣的:(python)