Table of Contents generated with DocToc
The codecs module is used to handle different character encodings used with Unicode text I/O.
A CodecInfo instance c has the following methods:
c.encode(s [, errors]). A stateless encoding function that encodes the Unicode string s and returns a tuple(bytes, length_consumed). bytes is an 8-bit string or byte-array containing the encoded data.c.decode(bytes [, errors]). A stateless encoding function that decodes a byte string bytes and returns a tuple(s, length_consumed).c.streamreader(bytestream [, errors]). Returns aStreamReaderinstance that is used to read encoded data.c.streamwriter(bytestream [, errors]). Returns aStreamWriterinstance that is used to write encoded data.c.incrementalencoder([errors]). Returns anIncrementalEncoderinstance that can be used to encode strings in multiple steps.c.incrementaldecoder([errors]). Returns anIncrementalDecoderinstance that can be used to decode byte strings in multiple steps.
The codecs module provides a collection of high-level functions that are used to simplify I/O involving encoded text.
open(filename, mode[, encoding[, errors[, buffering]]]). Opensfilenamein the givenmodeand provides transparent data encoding/decoding according to the encoding specified inencoding.EncodedFile(file, inputenc[, outputenc [, errors]]). A class that provides an encoding wrapper around an existing file object,file.
The re module is used to perform regular-expression pattern matching and replacement in strings. Both unicode and byte-strings are supported.
compile(str [, flags])
Compiles a regular-expression pattern string into a regular-expression object.
findall(pattern, string [,flags])
Returns a list of all nonoverlapping matches of pattern in string, including empty matches.
match(pattern, string [, flags])
Checks whether zero or more characters at the beginning of string match pattern.
search(pattern, string [, flags])
Searches string for the first match of pattern.
split(pattern, string [, maxsplit = 0])
Splits string by the occurrences of pattern.
sub(pattern, repl, string [, count = 0])
Replaces the leftmost nonoverlapping occurrences of pattern in string by using the replacement repl.
A compiled regular-expression object, r, created by the compile() function has the following methods and attributes.
r.patternThe pattern string from which the regular expression object was compiled.r.findall(string [, pos [, endpos]])Identical to thefindall()function.r.split(string [, maxsplit = 0])Identical to thesplit()`` function. r.sub(repl, string [, count = 0]) Identical to thesub()` function.r.search(string [, pos][, endpos])Searchesstringfor a match.r.match(string [, pos][, endpos])Checks whether zero or more characters at the beginning ofstringmatch.
The MatchObject instances returned by search() and match() contain information about the contents of groups as well as positional data about where matches occurred. A MatchObject instance, m, has the following methods and attributes:
m.group([group1, group2, ...])Returns one or more subgroups of the match.m.start([group]) m.end([group])These two methods return the indices of the start and end of the substring matched by a group.
The string module contains a number of useful constants and functions for manipulating strings. It also contains classes for implementing new string formatters.
The str.format() method of strings is used to perform advanced string formatting operations.
The string module defines a class Formatter that can be used to implement your own customized formatting operation.
f.parse(format_string)A function that creates an iterator for parsing the contents of the format stringformat_string.f.format(format_string, *args, **kwargs)Formats the stringformat_string.
The string module defines a new string type, Template, that simplifies certain string substitutions.
t.substitute(m [, **kwargs])This method takes a mapping object,m(for example, a dictionary), or a list of keyword arguments and performs a keyword substitution on the stringt.
The struct module is used to convert data between Python and binary data structures (represented as Python byte strings).
pack(fmt, v1, v2, ...)Packs the valuesv1,v2, and so on into a byte string according to the format string infmt.unpack(fmt, string)`` Unpacks the contents of a bytestringaccording to the format string infmt`.
The struct module defines a class Struct that provides an alternative interface for packing and unpacking. Using this class is more efficient because the format string is only interpreted once.
# Packs values into a byte string
s.pack(v1, v2, ...)
# Unpacks values from a bytes string
s.unpack(bytes)The unicodedata module provides access to the Unicode character database, which contains character properties for all Unicode characters.
Prev: 15. Data Strutures, Algorithms, and Code Simplification