Python库总结——配置相关库(ConfigParser,logging,sys,getopt)
来源:互联网 发布:新网域名转出到阿里云 编辑:程序博客网 时间:2024/06/06 19:46
一,ConfigParser(配置文件解析库)
Python的ConfigParser操作1
Python的ConfigParser操作2
三个函数区别
配置文件分析器
读写配置文件
这个是一个非常好的模块,因为不可能所有的参数或者名字都放在运行代码里,工程很大的话是不好管理和修改里面的参数或变量的,首先得找到然后才能修改,所以这是个非常good的模块。
Python的ConfigParser Module 中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。 RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的 解析。
配置文件格式如下:
中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
1: [db]2: db_host = 127.0.0.13: db_port = 224: db_user = root5: db_pass = rootroot6: 7: [concurrent]8: thread = 109: processor = 20
二,logging(日志记录库)
Python logging 日志详细配置
Python日志输出–logging模块
在现实生活中,记录日志非常重要。银行转账时会有转账记录;飞机飞行过程中,会有黑盒子(飞行数据记录器)记录飞行过程中的一切。如果有出现什么问题,人们可以通过日志数据来搞清楚到底发生了什么。
对于系统开发、调试以及运行,记录日志都是同样的重要。如果没有日志记录,程序崩溃时你几乎就没办法弄明白到底发生了什么事情。举个例子,当你在写一个服务器程序时,记录日志是非常有必要的。
json,yaml库
三,利用sys和getopt获得命令行参数
编写带命令行参数的Python程序
Python获得命令行参数的方法
sys模块
在 Python 中,sys 模块是一个非常常用且十分重要的模块,通过模块中的 sys.argv 就可以访问到所有的命令行参数,它的返回值是包含所有命令行参数的列表(list),下面我们通过程序来说明它的用法:
import sysprint 'Number of arguments:', len(sys.argv)print 'They are:', str(sys.argv)
运行以及结果:
python ./test_argv.py arg0 arg1 arg2Number of arguments: 4They are: ['./test_argv.py', 'arg0', 'arg1', 'arg2']
我们看到通过 sys.argv 我们可以获得运行 Python 程序中所有的命令行参数。
getops模块
getopt.getopt(args, options[, long_options])
我们先来看一下里面参数的含义:
args: 表示的是要被处理的命令行参数列表(通常是通过上述的 sys.argv 获得的结果)
options: 它表示的是命令行参数中的选项,通常是一个字母,就像我们在 Linux 中对于某个命令不熟悉时所使用的帮助选项-h一样。如果说该选项需要一个参数的话,需要在该字母后边加上一个冒号:,表示该选项需要一个参数(如果这句不明白可以看下边的示例程序)
long_options: 它是一个可选的参数,表示的是选项的长格式,上边的options是短格式,长格式的选项的参数格式示例为–input=input.txt,具体如何使用,详见下边的示例程序。
编写一个带命令行的 Python 程序
了解了 sys 模块和 getopt 模块,我们就可以来自己编写一个带有命令行的程序并且在该程序中,我们还使用了 getopt.GetoptError 来进行异常处理。代码如下:
# -*- coding:utf-8 -*- import sys, getoptdef main(argv): inputfile = "" outputfile = "" try: # 这里的 h 就表示该选项无参数,i:表示 i 选项后需要有参数 opts, args = getopt.getopt(argv, "hi:o:",["infile=", "outfile="]) except getopt.GetoptError: print 'Error: test_arg.py -i <inputfile> -o <outputfile>' print ' or: test_arg.py --infile=<inputfile> --outfile=<outputfile>' sys.exit(2) for opt, arg in opts: if opt == "-h": print 'test_arg.py -i <inputfile> -o <outputfile>' print 'or: test_arg.py --infile=<inputfile> --outfile=<outputfile>' sys.exit() elif opt in ("-i", "--infile"): inputfile = arg elif opt in ("-o", "--outfile"): outputfile = arg print 'Input file : ', inputfile print 'Output file: ', outputfileif __name__ == "__main__": main(sys.argv[1:])
运行结果:
./test_arg.pyInput file :Output file:
python ./test_arg.py -htest_arg.py -i <inputfile> -o <outputfile>or: test_arg.py --infile=<inputfile> --outfile=<outputfile>
python ./test_arg.py -aError: test_arg.py -i <inputfile> -o <outputfile> or: test_arg.py --infile=<inputfile> --outfile=<outputfile>
python ./test_arg.py -i in.txt -o out.txtInput file : in.txtOutput file: out.txt
Cookies python ./test_arg.py --infile=in.txtInput file : in.txtOutput file:
- Python库总结——配置相关库(ConfigParser,logging,sys,getopt)
- python configparser库
- Python—ConfigParser模块
- 【Python】标准库模板os, sys, platform, logging
- python学习—Day20—模块:logging、os、command、sys
- python库paramiko,ConfigParser实践
- python ConfigParser配置读写
- Python读写配置configparser
- python库之getopt
- Python logging(配置实现)
- Python文件配置configParser使用
- Python 模块学习 —— ConfigParser
- Python——configParser模块学习
- Python 读取写入配置文件 —— ConfigParser
- python的logging库
- Python中的logging库
- Python日志库logging
- Python的标准库sys、copy、os相关用法
- MongoDB_副本集
- Hadoop学习(四)——Hive学习2
- iCare Data Recovery Pro(数据恢复软件)官方破解版V8.0.5.0下载 | icare data recovery 注册码
- 关于android中cavas缩放函数scale后2个参数理解
- Android 使用updatefun 来自动更新
- Python库总结——配置相关库(ConfigParser,logging,sys,getopt)
- linux 练习五 如何生成库.so及被C和C++程序可用
- java分布式架构Dubbo+Zookeeper+ActiveMQ+Redis+Nginx+Netty+Oracle+RocketMQ+Linux,教学视频+项目实战
- 单点登录
- Integer to Roman--LeetCode
- hdu oj 1116 Play on Words(并查集+欧拉路径)
- Ubuntu下如何卸载Mysql数据库
- liunx 利用ssh传输文件
- 【Scikit-Learn 中文文档】密度估计