python中读取配置文件的方式
方式1:argparse
argparse,是Python标准库中推荐使用的编写命令行程序的工具。也可以用于读取配置文件。
字典样式的配置文件*.conf
配置文件test1.conf
{
"game0":
{
"ip":"127.0.0.1",
"port":27182,
"type":1
},
"game1":
{
"ip":"127.0.0.1",
"port":27183,
"type":0
},
"game2":
{
"ip":"127.0.0.1",
"port":27184,
"type":0
}
}
config.py
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: mytest.py
Description :
Author : andy9468
date: 2018/02/27
Copyright: (c) andy9468 2018
-------------------------------------------------
Change Activity:
2018/02/27:
-------------------------------------------------
"""
import json
import sys
import argparse def parse_args(args):
parser = argparse.ArgumentParser(prog="GameServer")
parser.add_argument('configfile', nargs=1, type=str, help='')
parser.add_argument('--game', default="game", type=str, help='')
return parser.parse_args(args) def parse(filename):
configfile = open(filename)
jsonconfig = json.load(configfile)
configfile.close()
return jsonconfig def main(argv):
args = parse_args(argv[1:])
print("args:", args)
config = parse(args.configfile[0])
info = config[args.game]
_ip = info['ip']
_port = info['port']
print("type:", type(_port))
_type = info['type']
print("print:%s,%d,%d" % (_ip, _port, _type)) if __name__ == '__main__':
main(sys.argv)
运行
启动脚本:python test.py test.conf --game=game0
详见:
http://blog.csdn.net/majianfei1023/article/details/49954705
方式2:ConfigParser
ConfigParser是Python读取conf配置文件标准的库。
中括号下设置子项的配置文件*.conf、或者*.ini
test2.conf
[game0]
ip = 127.0.0.1
port = 27182
type = 1 [game1]
ip = 127.0.0.1
port = 27183
type = 0 [game2]
ip = 127.0.0.1
port = 27184
type = 0
test2.py
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: test2.py
Description :
Author : andy9468
date: 2018/02/27
Copyright: (c) andy9468 2018
-------------------------------------------------
Change Activity:
2018/02/27:
-------------------------------------------------
"""
# -*- coding:utf-8 -*- import configparser
import sys def parse_args(filename):
cf = configparser.ConfigParser()
cf.read(filename) # return all sections
secs = cf.sections()
print("sections:", secs) # game0 section
game0 = cf.options("game0")
print("game0:", game0) items = cf.items("game0")
print("game0 items:", items) # read
_ip = cf.get("game0", "ip")
_port = cf.getint("game0", "port")
_type = cf.getint("game0", "type")
print("print:%s,%d,%d" % (_ip, _port, _type)) def main(argv):
parse_args(argv[1]) if __name__ == '__main__':
main(sys.argv)
print(sys.argv)
动态添加配置:
# add
cf.add_section('test3')
cf.set('test3','id','123')
cf.write(open(filename,'w'))
详见:
https://www.cnblogs.com/emily-qin/p/8022292.html
方式3:用变量(常量)作为配置文件格式。*.py
配置文件:config.py
LISTEN_PORT = 4444
USE_EPOLL = True
导入配置:myread.py
import config
port_num = config.LISTEN_PORT
if config.USE_EPOLL:
print(config.USE_EPOLL)
详见:
http://www.pythontip.com/blog/post/4912/
python中读取配置文件的方式的更多相关文章
- python中读取配置文件ConfigParser
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...
- 使用python中读取配置文件
最近在接触利用python来写测试框架,本人也是个刚接触python,所以是个小菜鸟,今天开始,一点点的记录学习中的积累,方便以后的学习以及回顾,也希望能帮助跟我一样的小菜鸟们一步步的成长起来.那么, ...
- python读取配置文件的方式
python读取配置文件的方式 1.从config.ini中读取,后缀无所谓,文件名字也无所谓,不过config.ini是常用写法,所谓见名知意 config.ini内容: [global] ip = ...
- java中读取配置文件ResourceBundle和Properties两种方式比较
今天在开发的时候,需要把一些信息放到配置文件中,方便后续的修改,注意到用的是ResourceBundle读取配置文件的方式,记得之前也见过使用Properties的方式,就比较好奇这两种方式的区别,网 ...
- JavaWeb中servlet读取配置文件的方式
我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...
- Java读取配置文件的方式
Java读取配置文件的方式-笔记 1 取当前启动文件夹下的配置文件 一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...
- .NET Core类库中读取配置文件
最近在开发基于.NET Core的NuGet包,遇到一个问题:.NET Core中已经没有ConfigurationManager类,在类库中无法像.NET Framework那样读取App.conf ...
- python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法
先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...
- 关于Python中读取写入文件并进行文件与用户交互的操作
一.提前知识点 在python中是同样和其他语言一样可以进行文件的读取写入操作,值得注意的是,Python中打开文件读取的方式有几种,分别是以下几种: f = open('username.txt') ...
随机推荐
- C#------各种数据库连接字符串编写
转载: https://www.connectionstrings.com/
- 06python 之基本数据类型
数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483646 在64位机器上,整数的位数为64位,取值范围为-2** ...
- 说说GPIO.H(NUC131)
/**************************************************************************//** * @file GPIO.h * @ve ...
- (三)微信小程序之发送服务通知(模板消息)
1.后端获取AccessToken返回给微信端 微信小程序端请求后端得到AccessToken 2.后端获取openid返回给微信端 微信小程序端登录请求后端得到openid 3.发送消息 ...
- PHP-Yii框架下提交表单form防止csrf攻击
解决办法: 在form标签下,写入一个隐藏的input控件 <form id="form_submit" action="http://v2admin.doneve ...
- JAVA Comparator 接口排序用法
java的比较器有两类,分别是Comparable接口和Comparator接口. 在为对象数组进行排序时,比较器的作用非常明显,首先来讲解Comparable接口. 让需要进行排序的对象实现Comp ...
- C++虚继承的概念[转]
C++中虚拟继承的概念 为了解决从不同途径继承来的同名的数据成员在内存中有不同的拷贝造成数据不一致问题,将共同基类设置为虚基类.这时从不同的路径继承过来的同名数据成员在内存中就只有一个拷贝,同一个函数 ...
- PropertyPlaceholderConfigurer读取配置文件
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceho ...
- Android 跨进程调用忽略权限
Framework层: @Override public StackInfo getStackInfo(int stackId) { final int callingUid = ...
- Tomcat7.0无法启动解决方法[failed to start]
很奇怪的一个问题,Tomcat一直好好的,运行Servlet之后就报这个错: 为什么呢?在网上查都查不到解决方法,后来仔细检查了下Servlet,发现web.xml有个低级错误: 配置的Servlet ...