[python]python标准库(模块)简介
2015-04-11 20:54
351 查看
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
pickle.dump(obj, file[, protocol])Write a pickled representation of obj to th
4000
e 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.
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.
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.
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:
None, True, and False
integers, long integers, floating point numbers, complex numbers
normal and Unicode strings
tuples, lists, sets, and dictionaries containing only picklable objects
functions defined at the top level of a module
built-in functions defined at the top level of a module
classes that are defined at the top level of a module
instances of such classes whose dict or the result of calling getstate() is picklable (see section The pickle protocol for details).
相关文章推荐
- Python标准库(非常经典的各种模块介绍)
- Python:pygame模块及SDL库简介
- python datetime模块 简介
- python标准库os模块学习
- python标准库fileinput模块学习
- Python标准库——os模块
- Python-Markdown 模块简介
- Python标准库(非常经典的各种模块介绍)
- python 模块简介
- python 常用模块简介和安装
- Python:pygame模块及SDL库简介
- python标准库logging模块代码分析
- Python笔记(5):sys模块,os模块,set,heap,deque简介
- Python标准库(非常经典的各种模块介绍)
- Python标准库简介
- Python标准库-traceback模块
- Python optparser模块简介
- 9、Python-模块和标准库
- Python标准库(非常经典的各种模块介绍)
- Python3.4 asyncio模块简介