Python3 配置文件(configparser)

Python3 配置文件(configparser)


本文由 Luzhuo 编写,转发请保留该信息.
原文: http://blog.csdn.net/rozol/article/details/72793304


以下代码以Python3.6.1为例
Less is more!
configparser 可以读写和解析注释文件, 但是没有写入注释的功能

#!/usr/bin/env python
# coding=utf-8
__author__ = 'Luzhuo'
__date__ = '2017/5/26'
# config_configparser.py 配置文件
# configparser 可以读写和解析注释文件, 但是没有写入注释的功能

import configparser
import re


config_str = '''
# 配置文件信息案例
[DEFAULT]
minSdkVersion = 15
targetSdkVersion = 24
versionName = 1.0.0
server action = yes

[luzhuo.me]
user = luzhuo

# This is a comments.
[mysql]
ip = 127.0.0.1
port = 3306
'''

def config_write():
    '''
    生成配置文件, 字典的形式添加数据
    '''

    config = configparser.ConfigParser()

    config['DEFAULT'] = {
  'minSdkVersion': '15',
                         'targetSdkVersion': '24',
                         'versionName': '1.0.0',
                         'server action': 'yes'}

    config['luzhuo.me'] = {}
    config['luzhuo.me']['user'] = 'luzhuo'

    config['mysql'] = {}
    topsecret = config['mysql']
    topsecret['ip'] = '127.0.0.1'
    topsecret['port'] = '3306'

    with open('config.ini', 'w') as configfile:
        config.write(configfile)


def conf
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值