Python 参数设置,,方式一:配置文件(C


方式一:配置文件(ConfigParser模块)

方式二:解析参数(argparse模块)

1.配置文件(ConfigParser模块)

1.1 ConfigParser简介

ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value的options内容。例如

[db]db_host = 127.0.0.1db_port = 22db_user = rootdb_pass = rootroot [concurrent]thread = 10processor = 20

1.2 ConfigParser 初始工作

使用ConfigParser 首选需要初始化实例,并读取配置文件:

import ConfigParsercf = ConfigParser.ConfigParser()cf.read("配置文件名")

1.3 ConfigParser函数

1.3.1. 获取所有sections

>>> s = cf.sections()>>> print s[‘db‘, ‘concurrent‘]

1.3.2 获得指定section的options

>>> cf.options(‘db‘)[‘db_host‘, ‘db_port‘, ‘db_user‘, ‘db_pass‘]

1.3.3 获得指定sections的配置信息

>>> cf.items(‘db‘)[(‘db_host‘, ‘127.0.0.1‘), (‘db_port‘, ‘22‘), (‘db_user‘, ‘root‘), (‘db_pass‘, ‘rootroot‘)]

1.3.4 获得指定sections的option的信息

>>> cf.get("db", "db_host")‘127.0.0.1‘>>> cf.getint("db", "db_port")22

同样有getfloat、getboolean

以下注意:凡是改变文件内容的,都要最后写入。

1.3.5 设置某个option的值

>>> cf.set("db", "db_host", "127.1.1.1")>>> cf.write(open("config.ini", ‘w‘))

写入后的文件内容为

[db]db_host = 127.1.1.1db_port = 22db_user = rootdb_pass = rootroot[concurrent]thread = 10processor = 20

1.3.6 添加一个section

>>> cf.add_section("jihite")>>> cf.set("jihite", "int", "15")>>> cf.set("jihite", "bool", "True")>>> cf.set("jihite", "float", "3.14")>>> cf.write(open("config.ini", ‘w‘))

写入后的文件内容为

[db]db_host = 127.1.1.1db_port = 22db_user = rootdb_pass = rootroot[concurrent]thread = 10processor = 20[jihite]int = 15bool = Truefloat = 3.14

1.3.7 移除一个section

>>> cf.remove_option("jihite", "int")True>>> cf.write(open("config.ini", ‘w‘))

改变后的文件为

[db]db_host = 127.1.1.1db_port = 22db_user = rootdb_pass = rootroot[concurrent]thread = 10processor = 20[jihite]bool = Truefloat = 3.14

1.3.8 移除一个option

>>> cf.remove_section("jihite")True>>> cf.write(open("config.ini", ‘w‘))

改变后的文件为

[db]db_host = 127.1.1.1db_port = 22db_user = rootdb_pass = rootroot[concurrent]thread = 10processor = 20  

1.4 ConfigParser举例

方式二:解析参数(argparse模块)

2.解析参数(argparse模块)

2.1 argparse简介

argparse是python的命令行解析工具,它是Python标准库中推荐使用的编写命令行程序的工具。

2.2 argparser 初始工作

import argparseparser = argparse.ArgumentParser()

类ArgumentParser定义为:

class ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars=‘-‘, fromfile_prefix_chars=None, argument_default=None, conflict_handler=‘error‘, add_help=True)

参数的含义为

2.2.1 prog:程序的名字,默认为sys.argv[0] 

>>> parser = argparse.ArgumentParser(prog="myprogram")>>> parser.print_help()usage: myprogram [-h]optional arguments:  -h, --help  show this help message and exit

2.2.2 usage: 描述程序用途的字符串

>>> parser = argparse.ArgumentParser(prog="myprogram", usage="%(prog)s [options]")>>> parser.print_help()usage: myprogram [options]optional arguments:  -h, --help  show this help message and exit

2.2.3 description: help信息前的文字

>>> parser.print_help()usage: myprogram [options]Create my own programoptional arguments:  -h, --help  show this help message and exit

2.2.4 epilog:help之后的文字

>>> parser = argparse.ArgumentParser(prog="myprogram", usage="%(prog)s [options]", description="Create my own program", epilog="And that‘s how you‘d foo a bar")>>> parser.print_help()usage: myprogram [options]Create my own programoptional arguments:  -h, --help  show this help message and exitAnd that‘s how you‘d foo a bar

2.2.5   

更详细参考

2.3 添加参数选项

http://www.cnblogs.com/linxiyue/p/3908623.html?utm_source=tuicool

Python 参数设置

相关内容

    暂无相关文章

评论关闭