016_python程序变量抽取配置的几种方式
一、json配置文件
json文件的互转,如下例子:
配置文件:example.json
{
"mysql":{
"host":"localhost",
"user":"root",
"passwd":"my secret password",
"db":"write-math"
},
"other":{
"preprocessing_queue":[
"preprocessing.scale_and_center",
"preprocessing.dot_reduction",
"preprocessing.connect_lines"
],
"use_anonymous":true
}
}
(1)把字典转换为json配置文件
with open('example.json') as json_data_file:
data = json.load(json_data_file)
print(data)
输出:
{u'other': {u'preprocessing_queue': [u'preprocessing.scale_and_center', u'preprocessing.dot_reduction', u'preprocessing.connect_lines'], u'use_anonymous': True}, u'mysql': {u'passwd': u'my secret password', u'host': u'localhost', u'db': u'write-math', u'user': u'root'}}
(2)再转化为文件:
with open('result.json', 'w') as fp:
json.dump(data, fp , indent=4)
输出:
{
"other": {
"preprocessing_queue": [
"preprocessing.scale_and_center",
"preprocessing.dot_reduction",
"preprocessing.connect_lines"
],
"use_anonymous": true
},
"mysql": {
"passwd": "my secret password",
"host": "localhost",
"db": "write-math",
"user": "root"
}
}
二、ini配置文件
config.ini
; config.ini
; Sample configuration file [installation]
library=%(prefix)s/lib
include=%(prefix)s/include
bin=%(prefix)s/bin
prefix=/usr/local # Setting related to debug configuration
[debug]
log_errors=true
show_warnings=False [server]
port: 8080
nworkers: 32
pid-file=/tmp/spam.pid
root=/www/root
signature:
=================================
Brought to you by the Python Cookbook
=================================
python test.py
#!/usr/bin/python
# -*- coding: UTF-8 -*- from configparser import ConfigParser
cfg = ConfigParser()
cfg.read('config.ini') # ['config.ini'] print cfg.sections() # [u'installation', u'debug', u'server']
print cfg.get('installation','library') # /usr/local/lib
print cfg.getboolean('debug', 'log_errors') # True
print cfg.getint('server','port') # 8080
print cfg.getint('server','nworkers') #32
print(cfg.get('server','signature'))
'''
=================================
Brought to you by the Python Cookbook
=================================
'''
016_python程序变量抽取配置的几种方式的更多相关文章
- 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比
[原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...
- spring配置datasource三种方式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...
- Spring事务配置的五种方式(转发)
Spring事务配置的五种方式(原博客地址是http://www.blogjava.net/robbie/archive/2009/04/05/264003.html)挺好的,收藏转发 前段时间对Sp ...
- Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别
转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之 ...
- [转] 微信小程序页面间通信的5种方式
微信小程序页面间通的5种方式 PageModel(页面模型)对小程序而言是很重要的一个概念,从app.json中也可以看到,小程序就是由一个个页面组成的. 如上图,这是一个常见结构的小程序:首页是一个 ...
- springboot 读取 yml 配置的几种方式
前言:在springboot 项目中一般默认的配置文件是application.properties,但是实际项目中我们一般会使用application.yml 文件,下面就介绍一下在springbo ...
- 【Spring】SpringMVC非注解配置的两种方式
目录结构: contents structure [+] SpringMVC是什么 Spring MVC的设计原理 SpringMVC配置的第一种方式 1,复制Jar包 2,Web.xml文件 3,M ...
- java之spring mvc之Controller配置的几种方式
这篇主要讲解 controller配置的几种方式. 1. URL对应 Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 <!-- 配置handlerMapping --> & ...
- spring配置datasource三种方式及具体信息
1.使用org.springframework.jdbc.datasource.DriverManagerDataSource说明:DriverManagerDataSource建立连接是只要有连接就 ...
随机推荐
- Ambari安装HDP问题:User root is not allowed to impersonate anonymous.User: hcat is not allowed to impersonate ambari-qa
User root is not allowed to impersonate anonymous 修改hadoop 配置文件 etc/hadoop/core-site.xml,加入如下配置项 < ...
- LOJ #6052. 「雅礼集训 2017 Day11」DIV
完了我是数学姿势越来越弱了,感觉这种CXRdalao秒掉的题我都要做好久 一些前置推导 首先我们很容易得出\((a+bi)(c+di)=k \Leftrightarrow ac-bd=k,ad+bc= ...
- jdk安装 java环境配置
登录http://www.oracle.com,下载JDK(J2SE) JDK 1.0,1.1,1.2,1.3,1.4 1.5 (JDK5.0) à支持注解.支持泛型 1.6(JDK6.0) à Se ...
- django插入数据库错误:mysql的1267错误
错误信息: django.db.utils.OperationalError: (1267, "Illegal mix of collations (latin1_swedish_ci,IM ...
- 《HelloGitHub》第 31 期
公告 网站新增了 Web 服务器排行榜.数据库排行榜 <HelloGitHub>第 31 期 兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣 ...
- Springboot整合Elastic-Job
Elastic-Job是当当网的任务调度开源框架,有以下功能 分布式调度协调 弹性扩容缩容 失效转移 错过执行作业重触发 作业分片一致性,保证同一分片在分布式环境中仅一个执行实例 自诊断并修复分布式不 ...
- 研究windows下SVN备份及还原恢复方案
windows下SVN备份方案 备份策略 svn备份一般采用三种方式: 1)svnadmin dump 2)svnadmin hotcopy 3)svnsync. 注意,svn备份不宜采用普通的 ...
- 谈谈axios配置请求头content-type
现在前端开发中需要通过Ajax发送请求获取后端数据是很普遍的一件事情了,鉴于我平时在撸码中用的是vue技术栈,今天这里来谈谈我们常用的发Ajax请求的一个插件-axios. > 现在网上可能发送 ...
- JavaScript引擎浅析
前言 该文章是为大家整理一个关于js的知识网络,重点是知识的罗列及之间的联系,所以实例可能会有所不足,导致可能没有对应知识的人看不懂,希望大家能够结合其他资料来学习这篇文章,并整理出自己的知识体系. ...
- 2019 Valentine's Day 圣地巡礼和WPR003N开箱刷U-boot记录
即兴打出这个标题,似乎性息量有点大,后面的内容真的和你想象的是一样的吗? 自上一篇blog告别这里有多少年了,掐指一算,今天是一个回归的日子,看着这里搭上云之路,渐渐的变成了云主机中的一员,感慨时事境 ...