首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python3 读取 ini 配置文件(

Python3 读取 ini 配置文件(

作者头像
py3study
发布2020-01-03 10:56:04
1.4K0
发布2020-01-03 10:56:04
举报
文章被收录于专栏:python3python3

【背景】

  Windows 的记事本会给 UTF-8 文件添加 BOM 头,很烦,搞个通用的读取配置文件的代码。可能报这种错误:

configparser.MissingSectionHeaderError: File contains no section headers.

file: 'D:\\Python3Project\\ClearWFWatermark\\config\\config.ini', line: 1

'\ufeff[config]\n'

【config.ini】

[config]
SrcRoot=D:\input
DstRoot=D:\output

【t.py】

#encoding=utf-8
#author: walker
#date: 2018-03-31
#summary: 读取 UTF-8/UTF-8-BOM 配置文件
 
import os, sys
from configparser import ConfigParser
 
SrcRoot = r''
DstRoot = r''
 
#读取配置文件
def ReadConfig(): 
	global SrcRoot, DstRoot
	 
	cfg = ConfigParser()
	# cfg.optionxform = str   # 保持键的大小写
	cfgFile = 'config.ini'
	if not os.path.exists(cfgFile):
		input(cfgFile + ' not found')
		sys.exit(-1)
	with open(cfgFile, mode='rb') as f:
		content = f.read()
	if content.startswith(b'\xef\xbb\xbf'):     # 去掉 utf8 bom 头
		content = content[3:]
	cfg.read_string(content.decode('utf8'))
	if not cfg.sections():
		input('Read config.ini failed...')
		sys.exit(-1)
		 
	SrcRoot = cfg.get('config', 'SrcRoot').strip()          
	if not os.path.exists(SrcRoot):
		print('Error: not exists %s' % SrcRoot)
		sys.exit(-1)
	print('SrcRoot: %s' % SrcRoot)
	 
	DstRoot = cfg.get('config', 'DstRoot').strip()          
	if not os.path.exists(DstRoot):
		print('Error: not exists %s' % DstRoot)
		sys.exit(-1)
	print('DstRoot: %s' % DstRoot)
		 
	print('Read config.ini successed!')
 
if __name__ == '__main__':
	ReadConfig()

【cmd】

λ python3 t.py
SrcRoot: D:\input
DstRoot: D:\output
Read config.ini successed!

【读取配置文件,并转换为二级有序字典】

# encoding: utf-8
# author: walker
# date: 2018-06-01
# summary: 读取配置文件,并转换为二级有序字典,结果其实类似于私有变量 cfg._sections

import os, sys
from collections import OrderedDict
from configparser import ConfigParser
from pprint import pprint

# 读取配置文件
# 返回一个二级字典
def ReadConfig():
    cfg = ConfigParser()
    cfg.optionxform = str   # 保持键的大小写
    cfgFile = 'config.ini'
    if not os.path.exists(cfgFile):
        input(cfgFile + ' not found')
        sys.exit(-1)
    with open(cfgFile, mode='rb') as f:
        content = f.read()
    if content.startswith(b'\xef\xbb\xbf'):     # 去掉 utf8 bom 头
        content = content[3:]
    cfg.read_string(content.decode('utf8'))
    if not cfg.sections():
        input('Read config.ini failed...')
        sys.exit(-1)
    
    dic = OrderedDict()
    for section in cfg.sections():
        dic[section] = OrderedDict()
        for option in cfg.options(section):
            dic[section][option] = cfg.get(section, option).strip()

    #pprint(dic) 
    print('Read %s completed!' % cfgFile)
    
    return dic

*** walker ***

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档