zoukankan      html  css  js  c++  java
  • python模块

    一. getopt

    注意for和if后面的 : ,它可以保证下行的正常缩进,而python就是根据缩进来执行代码的。

     1 #!/usr/bin/python3
     2 import sys
     3 import getopt
     4 
     5 options,args = getopt.getopt(sys.argv[1:], 'p:i:', ['ip = ', 'port = '])
     6 for name, value in options:
     7     if name in('-i', '--ip'):
     8         print 'ip is ---- ', value
     9     if name in('-p','--port'):
    10         print 'port is ---- ', value

    python中 getopt 模块是专门用来处理命令行参数的

    函数getopt(args, shortopts, longopts = [])

    参数args一般是sys.argv[1:]

    shortopts 短格式 (-) 

    longopts 长格式(--) 

    命令行中输入:

    python test.py -i 127.0.0.1 -p 80 55 66
    输出:
    ip is ---- 127.0.0.1
    port is ---- 80
    
    python test.py --ip=127.0.0.1 --port=80 55 66
    输出: ip is ---- =127.0.0.1 port is ---- =80

    二.ConfigParser

    ConfigParser模块get官方文档解释如下:
    The ConfigParser class extends some methods of the RawConfigParser interface, adding some optional arguments.

    ConfigParser.get(section, option[, raw[, vars]])
    Get an option value for the named section. If vars is provided, it must be a dictionary. The option is looked up in vars (if provided), section, and in defaults in that order.

    All the '%' interpolations are expanded in the return values, unless the raw argument is true. Values for interpolation keys are looked up in the same manner as the option.

    ConfigParser.items(section[, raw[, vars]])
    Return a list of (name, value) pairs for each option in the given section. Optional arguments have the same meaning as for the get() method.

    New in version 2.3.

    简单来说就是:获取命名部分的选项值
    ConfigParser.get(section,option [,raw [,vars]])
    section 配置名
    option 选项名
    raw bool类型 可选参数,默认为False 
    vars dict类型 可选参数

    如果提供了vars 那么获取配置选项值得规则如下
    先在vars中寻找,如果找到就使用vars中的值
    如果找不到 就是用默认值
    前提是raw的值是False

    以下是测试代码

    文件test.conf内容如下

    1 [Section1]
    2 foo=%(bar)s is %(baz)s!
    3 baz=fun
    4 bar=Python

    测试代码:

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 import ConfigParser
     5 import string, os
     6 cf = ConfigParser.ConfigParser()
     7 cf.read("test.conf")
     8 res = cf.get('Section1', 'foo')
     9 print "默认情况下, raw=False, 此时输出 %s" % res
    10 res = cf.get('Section1', 'foo', raw=False)
    11 print "raw=False, 无参数vars 此时等同于默认输出:%s" % res
    12 res = cf.get('Section1', 'foo', raw=True)
    13 print "raw=True, 无参数vars 此时等输出未被匹配原字符:%s" % res
    14 res = cf.get('Section1', 'foo', raw=False, vars={'bar': 'Documentation','baz': 'evil'})
    15 print "raw=False, vars存在 此时使用vars中的值进行匹配:%s" % res
    16 res = cf.get('Section1', 'foo', raw=True, vars={'bar': 'Documentation', 'baz':'sdsd'})
    17 print "raw=True, vars存在 此时vars不生效,输出未被匹配原字符:%s" % res
    18 res = cf.get('Section1', 'foo', raw=False, vars={'bar': 'Documentation'})
    19 print "raw=False, vars存在,但只包含一个值, 此时另一个值取默认匹配值,输出未:%s" % res

    输出如下

     输出,最后一行为raw = False

  • 相关阅读:
    移动端事件
    移动端的三种布局
    bootstrap自定义——栅格列数修改
    less文件的运行
    lessc的安装
    nodejs的安装
    jquery插件之jquery-ui
    指定网卡进行ping操作
    汇编语言从入门到精通-4标识符和表达式
    汇编语言从入门到精通-3操作数的寻址方式
  • 原文地址:https://www.cnblogs.com/Lunais/p/8862755.html
Copyright © 2011-2022 走看看