您的位置:首页 > 编程语言 > Python开发

[从头学python] 第05节 字符串模块string

2015-12-14 15:53 525 查看
本节目标:

(1) 学习标准库ctypes, functools, gettext, io, keyword, itertools, logging, mmap, re, string模块



实现步骤:

对c语言中的类型进行操作

from ctypes import *
>>> dir()
['ARRAY', 'ArgumentError', 'Array', 'BigEndianStructure', 'CDLL', 'CFUNCTYPE', 'DEFAULT_MODE', 'DllCanUnloadNow', 'DllGetClassObject', 'FormatError', 'GetLastError',
'HRESULT', 'LibraryLoader', 'LittleEndianStructure', 'OleDLL', 'POINTER', 'PYFUNCTYPE',
'PyDLL', 'RTLD_GLOBAL', 'RTLD_LOCAL', 'SetPointerType', 'Structure', 'Union', 'WINFUNCTYPE'
, 'WinDLL', 'WinError', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', 'addressof', 'alignment', 'byref', 'c_bool', 'c_buffer', 'c_byte', 'c_char', 'c_char_p',
'c_double', 'c_float', 'c_int', 'c_int16', 'c_int32', 'c_int64', 'c_int8', 'c_long',
'c_longdouble', 'c_longlong', 'c_short', 'c_size_t', 'c_ssize_t', 'c_ubyte', 'c_uint',
'c_uint16', 'c_uint32', 'c_uint64', 'c_uint8', 'c_ulong', 'c_ulonglong', 'c_ushort',
'c_void_p', 'c_voidp', 'c_wchar', 'c_wchar_p', 'cast', 'cdll', 'create_string_buffer',
'create_unicode_buffer', 'get_errno', 'get_last_error', 'memmove', 'memset', 'oledll',
'pointer', 'py_object', 'pydll', 'pythonapi', 'resize', 'set_errno', 'set_last_error',
'sizeof', 'string_at', 'windll', 'wstring_at']

>>> help(ctypes)
Help on package ctypes:

NAME
ctypes - create and manipulate C data types in Python

PACKAGE CONTENTS
_endian
macholib (package)
test (package)
util
wintypes

CLASSES
_ctypes._SimpleCData(_ctypes._CData)
HRESULT
c_bool
c_byte
c_char
c_char_p
c_double
c_float
c_long
c_longlong
c_short
c_ubyte
c_ulong
c_ulonglong
c_ushort
c_void_p
c_wchar
c_wchar_p
py_object
builtins.Exception(builtins.BaseException)
ArgumentError
builtins.object
CDLL
OleDLL
PyDLL
WinDLL
LibraryLoader


包装后的函数工具functools

>>> import functools
>>> dir(functools)
['RLock', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', '_CacheInfo', '_HashedSeq', '__all__',
'__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__',
'__name__', '__package__', '_make_key', 'cmp_to_key', 'lru_cache', 'namedtuple', 'partial',
'reduce', 'total_ordering', 'update_wrapper', 'wraps']

>>> help(functools)
Help on module functools:

NAME
functools - functools.py - Tools for working with functions and callable objects

CLASSES
builtins.object
partial

>>> functools.reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15


国际化的取文字串

>>> import gettext
>>> dir(gettext)
['Catalog', 'ENOENT', 'GNUTranslations', 'NullTranslations', '__all__', '__builtins__',
'__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__',
'__package__', '_current_domain', '_default_localedir', '_expand_lang', '_localecodesets',
'_localedirs', '_translations', 'bind_textdomain_codeset', 'bindtextdomain', 'c2py', 'copy',
'dgettext', 'dngettext', 'find', 'gettext', 'install', 'io', 'ldgettext', 'ldngettext',
'lgettext', 'lngettext', 'locale', 'ngettext', 'os', 're', 'struct', 'sys', 'textdomain',
'translation']

>>> help(gettext)
Help on module gettext:

NAME
gettext - Internationalization and localization support.

DESCRIPTION
This module provides internationalization (I18N) and localization (L10N)
support for your Python programs by providing an interface to the GNU gettext
message catalog library.

I18N refers to the operation by which a program is made aware of multiple
languages.  L10N refers to the adaptation of your program, once
internationalized, to the local language and cultural habits.

CLASSES
builtins.object
NullTranslations
GNUTranslations


流式输入输出的io

>>> import io
>>> dir(io)
['BlockingIOError', 'BufferedIOBase', 'BufferedRWPair', 'BufferedRandom', 'BufferedReader',
'BufferedWriter', 'BytesIO', 'DEFAULT_BUFFER_SIZE', 'FileIO', 'IOBase',
'IncrementalNewlineDecoder', 'OpenWrapper', 'RawIOBase', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET',
'StringIO', 'TextIOBase', 'TextIOWrapper', 'UnsupportedOperation', '__all__', '__author__',
'__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__',
'__name__', '__package__', '_io', 'abc', 'open']

>>> help(io)
Help on module io:

NAME
io

DESCRIPTION
The io module provides the Python interfaces to stream handling. The
builtin open function is defined in this module.

At the top of the I/O hierarchy is the abstract base class IOBase. It
defines the basic interface to a stream. Note, however, that there is no
separation between reading and writing to streams; implementations are
allowed to raise an IOError if they do not support a given operation.

Extending IOBase is RawIOBase which deals simply with the reading and
writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide
an interface to OS files.

BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its
subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer
streams that are readable, writable, and both respectively.
BufferedRandom provides a buffered interface to random access
streams. BytesIO is a simple stream of in-memory bytes.

Another IOBase subclass, TextIOBase, deals with the encoding and decoding
of streams into text. TextIOWrapper, which extends it, is a buffered text
interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO
is a in-memory stream for text.

Argument names are not part of the specification, and only the arguments
of open() are intended to be used as keyword arguments.

data:

DEFAULT_BUFFER_SIZE

An int containing the default buffer size used by the module's buffered
I/O classes. open() uses the file's blksize (as obtained by os.stat) if
possible.

CLASSES
_io._BufferedIOBase(_io._IOBase)
_io.BufferedRWPair
_io.BufferedRandom
_io.BufferedReader
_io.BufferedWriter
_io.BytesIO
BufferedIOBase(_io._BufferedIOBase, IOBase)
_io._IOBase(builtins.object)
IOBase
BufferedIOBase(_io._BufferedIOBase, IOBase)
RawIOBase(_io._RawIOBase, IOBase)
TextIOBase(_io._TextIOBase, IOBase)
_io._RawIOBase(_io._IOBase)
_io.FileIO
RawIOBase(_io._RawIOBase, IOBase)
_io._TextIOBase(_io._IOBase)
_io.StringIO
_io.TextIOWrapper
TextIOBase(_io._TextIOBase, IOBase)
builtins.OSError(builtins.Exception)
builtins.BlockingIOError
UnsupportedOperation(builtins.ValueError, builtins.OSError)
builtins.ValueError(builtins.Exception)
UnsupportedOperation(builtins.ValueError, builtins.OSError)

>>> dir(io.BufferedWriter)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__',
'__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__'
, '__init__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_dealloc_warn',
'close', 'closed', 'detach', 'fileno', 'flush', 'isatty', 'mode', 'name', 'raw', 'read',
'read1', 'readable', 'readinto', 'readline', 'readlines', 'seek', 'seekable', 'tell',
'truncate', 'writable', 'write', 'writelines']

>>> help(io.BufferedWriter)
Help on class BufferedWriter in module io:

class BufferedWriter(_BufferedIOBase)
|  A buffer for a writeable sequential RawIO object.
|
|  The constructor creates a BufferedWriter for the given writeable raw
|  stream. If the buffer_size is not given, it defaults to
|  DEFAULT_BUFFER_SIZE.
|
|  Method resolution order:
|      BufferedWriter
|      _BufferedIOBase
|      _IOBase
|      builtins.object
迭代工具itertools

>>> import itertools
>>> dir(itertools)
['__doc__', '__loader__', '__name__', '__package__', '_grouper', '_tee', '_tee_dataobject',
'accumulate', 'chain', 'combinations', 'combinations_with_replacement', 'compress', 'count',
'cycle', 'dropwhile', 'filterfalse', 'groupby', 'islice', 'permutations', 'product',
'repeat', 'starmap', 'takewhile', 'tee', 'zip_longest']

>>> help(itertools)
Help on built-in module itertools:

NAME
itertools - Functional tools for creating and using iterators.

DESCRIPTION
Infinite iterators:
count(start=0, step=1) --> start, start+step, start+2*step, ...
cycle(p) --> p0, p1, ... plast, p0, p1, ...
repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times

Iterators terminating on the shortest input sequence:
accumulate(p[, func]) --> p0, p0+p1, p0+p1+p2
chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ...
chain.from_iterable([p, q, ...]) --> p0, p1, ... plast, q0, q1, ...
compress(data, selectors) --> (d[0] if s[0]), (d[1] if s[1]), ...
dropwhile(pred, seq) --> seq
, seq[n+1], starting when pred fails
groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)
filterfalse(pred, seq) --> elements of seq where pred(elem) is False
islice(seq, [start,] stop [, step]) --> elements from
seq[start:stop:step]
starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...
tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n
takewhile(pred, seq) --> seq[0], seq[1], until pred fails
zip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ...

Combinatoric generators:
product(p, q, ... [repeat=1]) --> cartesian product
permutations(p[, r])
combinations(p, r)
combinations_with_replacement(p, r)

CLASSES
builtins.object
accumulate
chain
combinations
combinations_with_replacement
compress
count
cycle
dropwhile
filterfalse
groupby
islice
permutations
product
repeat
starmap
takewhile
zip_longest


关銉词keyword模块

>>> import keyword
>>> dir(keyword)
['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__',
'__loader__', '__name__', '__package__', 'iskeyword', 'kwlist', 'main']

>>> help(keyword)
Help on module keyword:

NAME
keyword - Keywords (from "graminit.c")

DESCRIPTION
This file is automatically generated; please don't muck it up!

To update the symbols in this file, 'cd' to the top directory of
the python source tree after building the interpreter and run:

./python Lib/keyword.py


日志工具logging

>>> import logging
>>> dir(logging)
['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR', 'FATAL', 'FileHandler',
'Filter', 'Filterer', 'Formatter', 'Handler', 'INFO', 'LogRecord', 'Logger', 'LoggerAdapter',
'Manager', 'NOTSET', 'NullHandler', 'PercentStyle', 'PlaceHolder', 'RootLogger',
'StrFormatStyle', 'StreamHandler', 'StringTemplateStyle', 'Template', 'WARN', 'WARNING',
'_STYLES', '_StderrHandler', '__all__', '__author__', '__builtins__', '__cached__',
'__date__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__',
'__package__', '__path__', '__status__', '__version__', '_acquireLock', '_addHandlerRef',
'_checkLevel', '_defaultFormatter', '_defaultLastResort', '_handlerList', '_handlers',
'_levelNames', '_lock', '_logRecordFactory', '_loggerClass', '_releaseLock',
'_removeHandlerRef', '_showwarning', '_srcfile', '_startTime', '_warnings_showwarning',
'addLevelName', 'atexit', 'basicConfig', 'captureWarnings', 'critical', 'currentframe',
'debug', 'disable', 'error', 'exception', 'fatal', 'getLevelName', 'getLogRecordFactory',
'getLogger', 'getLoggerClass', 'info', 'io', 'lastResort', 'log', 'logMultiprocessing',
'logProcesses', 'logThreads', 'makeLogRecord', 'os', 'raiseExceptions', 'root',
'setLogRecordFactory', 'setLoggerClass', 'shutdown', 'sys', 'threading', 'time',
'traceback', 'warn', 'warning', 'warnings', 'weakref']

>>> help(logging)
Help on package logging:

NAME
logging

DESCRIPTION
Logging package for Python. Based on PEP 282 and comments thereto in
comp.lang.python.

Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.

To use, simply 'import logging' and log away!

PACKAGE CONTENTS
config
handlers

CLASSES
builtins.object
BufferingFormatter
Filter
Formatter
LogRecord
LoggerAdapter
Filterer(builtins.object)
Handler
NullHandler
StreamHandler
FileHandler
Logger

>>> logging.warn('not good')
WARNING:root:not good


内存映射mmap

>>> import mmap
>>> dir(mmap)
['ACCESS_COPY', 'ACCESS_READ', 'ACCESS_WRITE', 'ALLOCATIONGRANULARITY', 'PAGESIZE',
'__doc__', '__loader__', '__name__', '__package__', 'error', 'mmap']

>>> help(mmap)
Help on built-in module mmap:

NAME
mmap

CLASSES
builtins.object
mmap

class mmap(builtins.object)
|  Windows: mmap(fileno, length[, tagname[, access[, offset]]])
|
|  Maps length bytes from the file specified by the file handle fileno,
|  and returns a mmap object.  If length is larger than the current size
|  of the file, the file is extended to contain length bytes.  If length
|  is 0, the maximum length of the map is the current size of the file,
|  except that if the file is empty Windows raises an exception (you cannot
|  create an empty mapping on Windows).


正则表达式re模块

>>> import re
>>> dir(re)
['A', 'ASCII', 'DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S',
'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__',
'__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__',
'__name__', '__package__', '__version__', '_alphanum_bytes', '_alphanum_str', '_cache',
'_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type', '_pickle', '_subx',
'compile', 'copyreg', 'error', 'escape', 'findall', 'finditer', 'functools', 'match',
'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']

>>> help(re)
Help on module re:

NAME
re - Support for regular expressions (RE).

DESCRIPTION
This module provides regular expression matching operations similar to
those found in Perl.  It supports both 8-bit and Unicode strings; both
the pattern and the strings being processed can contain null bytes and
characters outside the US ASCII range.

Regular expressions can contain both special and ordinary characters.
Most ordinary characters, like "A", "a", or "0", are the simplest
regular expressions; they simply match themselves.  You can
concatenate ordinary characters, so last matches the string 'last'.

The special characters are:
"."      Matches any character except a newline.
"^"      Matches the start of the string.
"$"      Matches the end of the string or just before the newline at
the end of the string.
"*"      Matches 0 or more (greedy) repetitions of the preceding RE.
Greedy means that it will match as many repetitions as possible.
"+"      Matches 1 or more (greedy) repetitions of the preceding RE.
"?"      Matches 0 or 1 (greedy) of the preceding RE.
*?,+?,?? Non-greedy versions of the previous three special characters.
{m,n}    Matches from m to n repetitions of the preceding RE.
{m,n}?   Non-greedy version of the above.
"\\"     Either escapes special characters or signals a special sequence.
[]       Indicates a set of characters.
A "^" as the first character indicates a complementing set.
"|"      A|B, creates an RE that will match either A or B.
(...)    Matches the RE inside the parentheses.
The contents can be retrieved or matched later in the string.
(?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
(?:...)  Non-grouping version of regular parentheses.
(?P<name>...) The substring matched by the group is accessible by name.
(?P=name)     Matches the text matched earlier by the group named name.
(?#...)  A comment; ignored.
(?=...)  Matches if ... matches next, but doesn't consume the string.
(?!...)  Matches if ... doesn't match next.
(?<=...) Matches if preceded by ... (must be fixed length).
(?<!...) Matches if not preceded by ... (must be fixed length).
(?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
the (optional) no pattern otherwise.

The special sequences consist of "\\" and a character from the list
below.  If the ordinary character is not on the list, then the
resulting RE will match the second character.
\number  Matches the contents of the group of the same number.
\A       Matches only at the start of the string.
\Z       Matches only at the end of the string.
\b       Matches the empty string, but only at the start or end of a word.
\B       Matches the empty string, but not at the start or end of a word.
\d       Matches any decimal digit; equivalent to the set [0-9] in
bytes patterns or string patterns with the ASCII flag.
In string patterns without the ASCII flag, it will match the whole
range of Unicode digits.
\D       Matches any non-digit character; equivalent to [^\d].
\s       Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
bytes patterns or string patterns with the ASCII flag.
In string patterns without the ASCII flag, it will match the whole
range of Unicode whitespace characters.
\S       Matches any non-whitespace character; equivalent to [^\s].
\w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
in bytes patterns or string patterns with the ASCII flag.
In string patterns without the ASCII flag, it will match the
range of Unicode alphanumeric characters (letters plus digits
plus underscore).
With LOCALE, it will match the set [0-9_] plus characters defined
as letters for the current locale.
\W       Matches the complement of \w.
\\       Matches a literal backslash.


字符串string模块

>>> import string
>>> dir(string)
['ChainMap', 'Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__cached__',
'__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_re',
'_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits',
'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']

>>> help(string)
Help on module string:

NAME
string - A collection of string constants.

DESCRIPTION
Public module variables:

whitespace -- a string containing all ASCII whitespace
ascii_lowercase -- a string containing all ASCII lowercase letters
ascii_uppercase -- a string containing all ASCII uppercase letters
ascii_letters -- a string containing all ASCII letters
digits -- a string containing all ASCII decimal digits
hexdigits -- a string containing all ASCII hexadecimal digits
octdigits -- a string containing all ASCII octal digits
punctuation -- a string containing all ASCII punctuation characters
printable -- a string containing all ASCII characters considered printable

CLASSES
builtins.object
Formatter
Template


本节就到这里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: