Python3:读取配置dbconfig.ini(含有中文)显示乱码的解决方法

Python3:读取配置dbconfig.ini(含有中文)显示乱码的解决方法

一、原因

Python 3 中虽有encoding 参数,但是对于有BOM(如Windows下用记事本指定为utf-8)的文件,需要使用 utf-8-sig, 使用utf-8没办法。

二、解决方案

# -*- coding:utf-8 -*-
import configparser
# 生成config对象
config = configparser.ConfigParser()
# 读取配置文件(此处是utf-8-sig,而不是utf-8)
config.read('dbconfig.ini',encoding="utf-8-sig") 
print(config['dbemial']['sign_company'])
原文地址:https://www.cnblogs.com/lizm166/p/9328966.html