[python]python标准库(模块)简介

fnmatch

文件名匹配模块

fnmatch.fnmatch(filename,pattern)

pattern 含义
* 匹配任意字符
匹配单个字符
[seq] 匹配seq中的某一个字符
[!seq] 匹配不在seq中的任一字符

glob

待补充

getopt

获取命令行参数的模块

getopt.getopt(args, options[, long_options])
args一般等于sys.argv[1:]
options:字符串类型
long_options:字符串列表

>>> import getopt
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
>>> args
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'abc:d:')
>>> optlist
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> args
['a1', 'a2']

冒号’:’表示该option有参数(value),如上面的例子中,’abc:d:’,选型-c和-d都有参数。注意:命令行参数中不一定要全部包含a,b,c,d四个选项。

>>> optlist,args = getopt.getopt(['-a','-c','ccccc'],'abc:d:')
>>> optlist
[('-a', ''), ('-c', 'ccccc')]

long_options的例子

>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
>>> args = s.split()
>>> args
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'x', [
...     'condition=', 'output-file=', 'testing'])
>>> optlist
[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
>>> args
['a1', 'a2']

getopt能够解析形如’-abcd’的命令行参数形式,如:

>>> optlist,args = getopt.getopt(['-abcd'],'abcd')
>>>> optlist
[('-a', ''), ('-b', ''), ('-c', ''), ('-d', '')]

argparse

命令行参数解析模块,比getopt要高级,是一个非常强有力的命令行参数解析工具

argparse将参数分为两类:位置参数和可选参数

import argparse
parser = argparse.ArgumentParser()
#positional arguments
#parser.add_argument('echo',type=str,help='echo the string you use there')
parser.add_argument('square',type=int,help='display a square of a given number')

#optional arguments
#parser.add_argument('--verbose',help='increase output verbosity',
#                    action='store_true')
#shorted argument
parser.add_argument('-V','-v','--verbose',type=int,choices=[0,1,2],
                    help='increase output verbosity')
args = parser.parse_args()
answer = args.square ** 2
if args.verbose == 0:
    print 'the square of {0} equals {1}'.format(args.square,answer)
elif args.verbose == 1:
    print '{}^2 = {}'.format(args.square,answer)
else:
    print answer
#print args.echo
#print args.square ** 2
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('x',type=int,help='the base')
parser.add_argument('y',type=int,help='the exponent')
parser.add_argument('-V','--verbosity',action='count',default=0)
args = parser.parse_args()


answer = args.x ** args.y
if args.verbosity >= 2:
    print 'running %s' % __file__
if args.verbosity >= 1:
    print '%s^%s =' % (args.x,args.y),
print answer

具体用法参考python官方手册

marshal和pickle

数据序列化
dumps(value)—->字符串
loads(string)—–>value

marshal

marshal的用法和pickle基本类似,只是支持转换的数据不通。

pickle

  1. pickle.dump(obj, file[, protocol])

    Write a pickled representation of obj to the open file object file. This is equivalent to Pickler(file, protocol).dump(obj).

    If the protocol parameter is omitted, protocol 0 is used. If protocol is specified as a negative value or HIGHEST_PROTOCOL, the highest protocol version will be used.

    file must have a write() method that accepts a single string argument. It can thus be a file object opened for writing, a StringIO object, or any other custom object that meets this interface.

  2. pickle.load(file)

    Read a string from the open file object file and interpret it as a pickle data stream, reconstructing and returning the original object hierarchy. This is equivalent to Unpickler(file).load().

    file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return a string. Thus file can be a file object opened for reading, a StringIO object, or any other custom object that meets this interface.

    This function automatically determines whether the data stream was written in binary mode or not.

  3. pickle.dumps(obj[, protocol])

    Return the pickled representation of the object as a string, instead of writing it to a file.

    If the protocol parameter is omitted, protocol 0 is used. If protocol is specified as a negative value or HIGHEST_PROTOCOL, the highest protocol version will be used.

    Changed in version 2.3: The protocol parameter was added.

  4. pickle.loads(string)

    Read a pickled object hierarchy from a string. Characters in the string past the pickled object’s representation are ignored.

The following types can be pickled:

  1. None, True, and False
  2. integers, long integers, floating point numbers, complex numbers
  3. normal and Unicode strings
  4. tuples, lists, sets, and dictionaries containing only picklable objects
  5. functions defined at the top level of a module
  6. built-in functions defined at the top level of a module
  7. classes that are defined at the top level of a module
  8. instances of such classes whose dict or the result of calling getstate() is picklable (see section The pickle protocol for details).

你可能感兴趣的:(Python学习笔记,python)