首页 技术 正文
技术 2022年11月13日
0 收藏 404 点赞 3,211 浏览 48756 个字

博主所有python文章均为python3.5.1环境。

目录:

1.collections

2.subprocess

3.configparser

4.XML

5.shutil

一、 collections

  collections模块提供了一些特殊的数据结构类型,在某些场景下有着非常神奇的功效。

1、计数器(counter)

Counter是对字典类型的补充,用于追踪值的出现次数。

ps:具备字典的所有功能 + 自己的功能

c = Counter('abcdeabcdabcaba')print c输出:Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
 ######################################################################## ###  Counter ######################################################################## class Counter(dict):     '''Dict subclass for counting hashable items.  Sometimes called a bag     or multiset.  Elements are stored as dictionary keys and their counts     are stored as dictionary values.     >>> c = Counter('abcdeabcdabcaba')  # count elements from a string     >>> c.most_common(3)                # three most common elements     [('a', 5), ('b', 4), ('c', 3)]     >>> sorted(c)                       # list all unique elements     ['a', 'b', 'c', 'd', 'e']     >>> ''.join(sorted(c.elements()))   # list elements with repetitions     'aaaaabbbbcccdde'     >>> sum(c.values())                 # total of all counts     >>> c['a']                          # count of letter 'a'     >>> for elem in 'shazam':           # update counts from an iterable     ...     c[elem] += 1                # by adding 1 to each element's count     >>> c['a']                          # now there are seven 'a'     >>> del c['b']                      # remove all 'b'     >>> c['b']                          # now there are zero 'b'     >>> d = Counter('simsalabim')       # make another counter     >>> c.update(d)                     # add in the second counter     >>> c['a']                          # now there are nine 'a'     >>> c.clear()                       # empty the counter     >>> c     Counter()     Note:  If a count is set to zero or reduced to zero, it will remain     in the counter until the entry is deleted or the counter is cleared:     >>> c = Counter('aaabbc')     >>> c['b'] -= 2                     # reduce the count of 'b' by two     >>> c.most_common()                 # 'b' is still in, but its count is zero     [('a', 3), ('c', 1), ('b', 0)]     '''     # References:     #   http://en.wikipedia.org/wiki/Multiset     #   http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html     #   http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm     #   http://code.activestate.com/recipes/259174/     #   Knuth, TAOCP Vol. II section 4.6.3     def __init__(self, iterable=None, **kwds):         '''Create a new, empty Counter object.  And if given, count elements         from an input iterable.  Or, initialize the count from another mapping         of elements to their counts.         >>> c = Counter()                           # a new, empty counter         >>> c = Counter('gallahad')                 # a new counter from an iterable         >>> c = Counter({'a': 4, 'b': 2})           # a new counter from a mapping         >>> c = Counter(a=4, b=2)                   # a new counter from keyword args         '''         super(Counter, self).__init__()         self.update(iterable, **kwds)     def __missing__(self, key):         """ 对于不存在的元素,返回计数器为0 """         'The count of elements not in the Counter is zero.'         # Needed so that self[missing_item] does not raise KeyError         return 0     def most_common(self, n=None):         """ 数量大于等n的所有元素和计数器 """         '''List the n most common elements and their counts from the most         common to the least.  If n is None, then list all element counts.         >>> Counter('abcdeabcdabcaba').most_common(3)         [('a', 5), ('b', 4), ('c', 3)]         '''         # Emulate Bag.sortedByCount from Smalltalk         if n is None:             return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)         return _heapq.nlargest(n, self.iteritems(), key=_itemgetter(1))     def elements(self):         """ 计数器中的所有元素,注:此处非所有元素集合,而是包含所有元素集合的迭代器 """         '''Iterator over elements repeating each as many times as its count.         >>> c = Counter('ABCABC')         >>> sorted(c.elements())         ['A', 'A', 'B', 'B', 'C', 'C']         # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1         >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})         >>> product = 1         >>> for factor in prime_factors.elements():     # loop over factors         ...     product *= factor                       # and multiply them         >>> product         Note, if an element's count has been set to zero or is a negative         number, elements() will ignore it.         '''         # Emulate Bag.do from Smalltalk and Multiset.begin from C++.         return _chain.from_iterable(_starmap(_repeat, self.iteritems()))     # Override dict methods where necessary     @classmethod     def fromkeys(cls, iterable, v=None):         # There is no equivalent method for counters because setting v=1         # means that no element can have a count greater than one.         raise NotImplementedError(             'Counter.fromkeys() is undefined.  Use Counter(iterable) instead.')     def update(self, iterable=None, **kwds):         """ 更新计数器,其实就是增加;如果原来没有,则新建,如果有则加一 """         '''Like dict.update() but add counts instead of replacing them.         Source can be an iterable, a dictionary, or another Counter instance.         >>> c = Counter('which')         >>> c.update('witch')           # add elements from another iterable         >>> d = Counter('watch')         >>> c.update(d)                 # add elements from another counter         >>> c['h']                      # four 'h' in which, witch, and watch         '''         # The regular dict.update() operation makes no sense here because the         # replace behavior results in the some of original untouched counts         # being mixed-in with all of the other counts for a mismash that         # doesn't have a straight-forward interpretation in most counting         # contexts.  Instead, we implement straight-addition.  Both the inputs         # and outputs are allowed to contain zero and negative counts.         if iterable is not None:             if isinstance(iterable, Mapping):                 if self:                     self_get = self.get                     for elem, count in iterable.iteritems():                         self[elem] = self_get(elem, 0) + count                 else:                     super(Counter, self).update(iterable) # fast path when counter is empty             else:                 self_get = self.get                 for elem in iterable:                     self[elem] = self_get(elem, 0) + 1         if kwds:             self.update(kwds)     def subtract(self, iterable=None, **kwds):         """ 相减,原来的计数器中的每一个元素的数量减去后添加的元素的数量 """         '''Like dict.update() but subtracts counts instead of replacing them.         Counts can be reduced below zero.  Both the inputs and outputs are         allowed to contain zero and negative counts.         Source can be an iterable, a dictionary, or another Counter instance.         >>> c = Counter('which')         >>> c.subtract('witch')             # subtract elements from another iterable         >>> c.subtract(Counter('watch'))    # subtract elements from another counter         >>> c['h']                          # 2 in which, minus 1 in witch, minus 1 in watch         >>> c['w']                          # 1 in which, minus 1 in witch, minus 1 in watch         -1         '''         if iterable is not None:             self_get = self.get             if isinstance(iterable, Mapping):                 for elem, count in iterable.items():                     self[elem] = self_get(elem, 0) - count             else:                 for elem in iterable:                     self[elem] = self_get(elem, 0) - 1         if kwds:             self.subtract(kwds)     def copy(self):         """ 拷贝 """         'Return a shallow copy.'         return self.__class__(self)     def __reduce__(self):         """ 返回一个元组(类型,元组) """         return self.__class__, (dict(self),)     def __delitem__(self, elem):         """ 删除元素 """         'Like dict.__delitem__() but does not raise KeyError for missing values.'         if elem in self:             super(Counter, self).__delitem__(elem)     def __repr__(self):         if not self:             return '%s()' % self.__class__.__name__         items = ', '.join(map('%r: %r'.__mod__, self.most_common()))         return '%s({%s})' % (self.__class__.__name__, items)     # Multiset-style mathematical operations discussed in:     #       Knuth TAOCP Volume II section 4.6.3 exercise 19     #       and at http://en.wikipedia.org/wiki/Multiset     #     # Outputs guaranteed to only include positive counts.     #     # To strip negative and zero counts, add-in an empty counter:     #       c += Counter()     def __add__(self, other):         '''Add counts from two counters.         >>> Counter('abbb') + Counter('bcc')         Counter({'b': 4, 'c': 2, 'a': 1})         '''         if not isinstance(other, Counter):             return NotImplemented         result = Counter()         for elem, count in self.items():             newcount = count + other[elem]             if newcount > 0:                 result[elem] = newcount         for elem, count in other.items():             if elem not in self and count > 0:                 result[elem] = count         return result     def __sub__(self, other):         ''' Subtract count, but keep only results with positive counts.         >>> Counter('abbbc') - Counter('bccd')         Counter({'b': 2, 'a': 1})         '''         if not isinstance(other, Counter):             return NotImplemented         result = Counter()         for elem, count in self.items():             newcount = count - other[elem]             if newcount > 0:                 result[elem] = newcount         for elem, count in other.items():             if elem not in self and count < 0:                 result[elem] = 0 - count         return result     def __or__(self, other):         '''Union is the maximum of value in either of the input counters.         >>> Counter('abbb') | Counter('bcc')         Counter({'b': 3, 'c': 2, 'a': 1})         '''         if not isinstance(other, Counter):             return NotImplemented         result = Counter()         for elem, count in self.items():             other_count = other[elem]             newcount = other_count if count < other_count else count             if newcount > 0:                 result[elem] = newcount         for elem, count in other.items():             if elem not in self and count > 0:                 result[elem] = count         return result     def __and__(self, other):         ''' Intersection is the minimum of corresponding counts.         >>> Counter('abbb') & Counter('bcc')         Counter({'b': 1})         '''         if not isinstance(other, Counter):             return NotImplemented         result = Counter()         for elem, count in self.items():             other_count = other[elem]             newcount = count if count < other_count else other_count             if newcount > 0:                 result[elem] = newcount         return result Counter Counter

counter

2、有序字典(orderedDict )

orderdDict是对字典类型的补充,他记住了字典元素添加的顺序

 class OrderedDict(dict):     'Dictionary that remembers insertion order'     # An inherited dict maps keys to values.     # The inherited dict provides __getitem__, __len__, __contains__, and get.     # The remaining methods are order-aware.     # Big-O running times for all methods are the same as regular dictionaries.     # The internal self.__map dict maps keys to links in a doubly linked list.     # The circular doubly linked list starts and ends with a sentinel element.     # The sentinel element never gets deleted (this simplifies the algorithm).     # Each link is stored as a list of length three:  [PREV, NEXT, KEY].     def __init__(self, *args, **kwds):         '''Initialize an ordered dictionary.  The signature is the same as         regular dictionaries, but keyword arguments are not recommended because         their insertion order is arbitrary.         '''         if len(args) > 1:             raise TypeError('expected at most 1 arguments, got %d' % len(args))         try:             self.__root         except AttributeError:             self.__root = root = []                     # sentinel node             root[:] = [root, root, None]             self.__map = {}         self.__update(*args, **kwds)     def __setitem__(self, key, value, dict_setitem=dict.__setitem__):         'od.__setitem__(i, y) <==> od[i]=y'         # Setting a new item creates a new link at the end of the linked list,         # and the inherited dictionary is updated with the new key/value pair.         if key not in self:             root = self.__root             last = root[0]             last[1] = root[0] = self.__map[key] = [last, root, key]         return dict_setitem(self, key, value)     def __delitem__(self, key, dict_delitem=dict.__delitem__):         'od.__delitem__(y) <==> del od[y]'         # Deleting an existing item uses self.__map to find the link which gets         # removed by updating the links in the predecessor and successor nodes.         dict_delitem(self, key)         link_prev, link_next, _ = self.__map.pop(key)         link_prev[1] = link_next                        # update link_prev[NEXT]         link_next[0] = link_prev                        # update link_next[PREV]     def __iter__(self):         'od.__iter__() <==> iter(od)'         # Traverse the linked list in order.         root = self.__root         curr = root[1]                                  # start at the first node         while curr is not root:             yield curr[2]                               # yield the curr[KEY]             curr = curr[1]                              # move to next node     def __reversed__(self):         'od.__reversed__() <==> reversed(od)'         # Traverse the linked list in reverse order.         root = self.__root         curr = root[0]                                  # start at the last node         while curr is not root:             yield curr[2]                               # yield the curr[KEY]             curr = curr[0]                              # move to previous node     def clear(self):         'od.clear() -> None.  Remove all items from od.'         root = self.__root         root[:] = [root, root, None]         self.__map.clear()         dict.clear(self)     # -- the following methods do not depend on the internal structure --     def keys(self):         'od.keys() -> list of keys in od'         return list(self)     def values(self):         'od.values() -> list of values in od'         return [self[key] for key in self]     def items(self):         'od.items() -> list of (key, value) pairs in od'         return [(key, self[key]) for key in self]     def iterkeys(self):         'od.iterkeys() -> an iterator over the keys in od'         return iter(self)     def itervalues(self):         'od.itervalues -> an iterator over the values in od'         for k in self:             yield self[k]     def iteritems(self):         'od.iteritems -> an iterator over the (key, value) pairs in od'         for k in self:             yield (k, self[k])     update = MutableMapping.update     __update = update # let subclasses override update without breaking __init__     __marker = object()     def pop(self, key, default=__marker):         '''od.pop(k[,d]) -> v, remove specified key and return the corresponding         value.  If key is not found, d is returned if given, otherwise KeyError         is raised.         '''         if key in self:             result = self[key]             del self[key]             return result         if default is self.__marker:             raise KeyError(key)         return default     def setdefault(self, key, default=None):         'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'         if key in self:             return self[key]         self[key] = default         return default     def popitem(self, last=True):         '''od.popitem() -> (k, v), return and remove a (key, value) pair.         Pairs are returned in LIFO order if last is true or FIFO order if false.         '''         if not self:             raise KeyError('dictionary is empty')         key = next(reversed(self) if last else iter(self))         value = self.pop(key)         return key, value     def __repr__(self, _repr_running={}):         'od.__repr__() <==> repr(od)'         call_key = id(self), _get_ident()         if call_key in _repr_running:             return '...'         _repr_running[call_key] = 1         try:             if not self:                 return '%s()' % (self.__class__.__name__,)             return '%s(%r)' % (self.__class__.__name__, self.items())         finally:             del _repr_running[call_key]     def __reduce__(self):         'Return state information for pickling'         items = [[k, self[k]] for k in self]         inst_dict = vars(self).copy()         for k in vars(OrderedDict()):             inst_dict.pop(k, None)         if inst_dict:             return (self.__class__, (items,), inst_dict)         return self.__class__, (items,)     def copy(self):         'od.copy() -> a shallow copy of od'         return self.__class__(self)     @classmethod     def fromkeys(cls, iterable, value=None):         '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.         If not specified, the value defaults to None.         '''         self = cls()         for key in iterable:             self[key] = value         return self     def __eq__(self, other):         '''od.__eq__(y) <==> od==y.  Comparison to another OD is order-sensitive         while comparison to a regular mapping is order-insensitive.         '''         if isinstance(other, OrderedDict):             return dict.__eq__(self, other) and all(_imap(_eq, self, other))         return dict.__eq__(self, other)     def __ne__(self, other):         'od.__ne__(y) <==> od!=y'         return not self == other     # -- the following methods support python 3.x style dictionary views --     def viewkeys(self):         "od.viewkeys() -> a set-like object providing a view on od's keys"         return KeysView(self)     def viewvalues(self):         "od.viewvalues() -> an object providing a view on od's values"         return ValuesView(self)     def viewitems(self):         "od.viewitems() -> a set-like object providing a view on od's items"         return ItemsView(self) OrderedDict

orderedDict

3、默认字典(defaultdict)

学前需求:

有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。

即: {'k1': 大于66 'k2': 小于66}原生字典解决办法:

values = [11, 22, 33,44,55,66,77,88,99,90]my_dict = {}for value in  values:    if value>66:        if my_dict.has_key('k1'):            my_dict['k1'].append(value)        else:            my_dict['k1'] = [value]    else:        if my_dict.has_key('k2'):            my_dict['k2'].append(value)        else:            my_dict['k2'] = [value]

  默认字典解决办法:

from collections import defaultdictvalues = [11, 22, 33,44,55,66,77,88,99,90]my_dict = defaultdict(list)for value in  values:    if value>66:        my_dict['k1'].append(value)    else:        my_dict['k2'].append(value)

  defaultdict是对字典的类型的补充,他默认给字典的值设置了一个类型。

 class defaultdict(dict):     """     defaultdict(default_factory[, ...]) --> dict with default factory     The default factory is called without arguments to produce     a new value when a key is not present, in __getitem__ only.     A defaultdict compares equal to a dict with the same items.     All remaining arguments are treated the same as if they were     passed to the dict constructor, including keyword arguments.     """     def copy(self): # real signature unknown; restored from __doc__         """ D.copy() -> a shallow copy of D. """         pass     def __copy__(self, *args, **kwargs): # real signature unknown         """ D.copy() -> a shallow copy of D. """         pass     def __getattribute__(self, name): # real signature unknown; restored from __doc__         """ x.__getattribute__('name') <==> x.name """         pass     def __init__(self, default_factory=None, **kwargs): # known case of _collections.defaultdict.__init__         """         defaultdict(default_factory[, ...]) --> dict with default factory         The default factory is called without arguments to produce         a new value when a key is not present, in __getitem__ only.         A defaultdict compares equal to a dict with the same items.         All remaining arguments are treated the same as if they were         passed to the dict constructor, including keyword arguments.         # (copied from class doc)         """         pass     def __missing__(self, key): # real signature unknown; restored from __doc__         """         __missing__(key) # Called by __getitem__ for missing key; pseudo-code:           if self.default_factory is None: raise KeyError((key,))           self[key] = value = self.default_factory()           return value         """         pass     def __reduce__(self, *args, **kwargs): # real signature unknown         """ Return state information for pickling. """         pass     def __repr__(self): # real signature unknown; restored from __doc__         """ x.__repr__() <==> repr(x) """         pass     default_factory = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default     """Factory for default value called by __missing__().""" defaultdict

defaultDict

4、可命名元组(namedtuple)

根据nametuple可以创建一个包含tuple所有功能以及其他功能的类型 

import collectionsMytuple = collections.namedtuple('Mytuple',['x', 'y', 'z'])
 class Mytuple(__builtin__.tuple)  |  Mytuple(x, y)  |  |  Method resolution order:  |      Mytuple  |      __builtin__.tuple  |      __builtin__.object  |  |  Methods defined here:  |  |  __getnewargs__(self)  |      Return self as a plain tuple.  Used by copy and pickle.  |  |  __getstate__(self)  |      Exclude the OrderedDict from pickling  |  |  __repr__(self)  |      Return a nicely formatted representation string  |  |  _asdict(self)  |      Return a new OrderedDict which maps field names to their values  |  |  _replace(_self, **kwds)  |      Return a new Mytuple object replacing specified fields with new values  |  |  ----------------------------------------------------------------------  |  Class methods defined here:  |  |  _make(cls, iterable, new=<built-in method __new__ of type object>, len=<built-in function len>) from __builtin__.type  |      Make a new Mytuple object from a sequence or iterable  |  |  ----------------------------------------------------------------------  |  Static methods defined here:  |  |  __new__(_cls, x, y)  |      Create new instance of Mytuple(x, y)  |  |  ----------------------------------------------------------------------  |  Data descriptors defined here:  |  |  __dict__  |      Return a new OrderedDict which maps field names to their values  |  |  x  |      Alias for field number 0  |  |  y  |      Alias for field number 1  |  |  ----------------------------------------------------------------------  |  Data and other attributes defined here:  |  |  _fields = ('x', 'y')  |  |  ----------------------------------------------------------------------  |  Methods inherited from __builtin__.tuple:  |  |  __add__(...)  |      x.__add__(y) <==> x+y  |  |  __contains__(...)  |      x.__contains__(y) <==> y in x  |  |  __eq__(...)  |      x.__eq__(y) <==> x==y  |  |  __ge__(...)  |      x.__ge__(y) <==> x>=y  |  |  __getattribute__(...)  |      x.__getattribute__('name') <==> x.name  |  |  __getitem__(...)  |      x.__getitem__(y) <==> x[y]  |  |  __getslice__(...)  |      x.__getslice__(i, j) <==> x[i:j]  |  |      Use of negative indices is not supported.  |  |  __gt__(...)  |      x.__gt__(y) <==> x>y  |  |  __hash__(...)  |      x.__hash__() <==> hash(x)  |  |  __iter__(...)  |      x.__iter__() <==> iter(x)  |  |  __le__(...)  |      x.__le__(y) <==> x<=y  |  |  __len__(...)  |      x.__len__() <==> len(x)  |  |  __lt__(...)  |      x.__lt__(y) <==> x<y  |  |  __mul__(...)  |      x.__mul__(n) <==> x*n  |  |  __ne__(...)  |      x.__ne__(y) <==> x!=y  |  |  __rmul__(...)  |      x.__rmul__(n) <==> n*x  |  |  __sizeof__(...)  |      T.__sizeof__() -- size of T in memory, in bytes  |  |  count(...)  |      T.count(value) -> integer -- return number of occurrences of value  |  |  index(...)  |      T.index(value, [start, [stop]]) -> integer -- return first index of value.  |      Raises ValueError if the value is not present. Mytuple Mytuple

namedtuple

5、双向队列(deque)

一个线程安全的双向队列

 class deque(object):     """     deque([iterable[, maxlen]]) --> deque object     Build an ordered collection with optimized access from its endpoints.     """     def append(self, *args, **kwargs): # real signature unknown         """ Add an element to the right side of the deque. """         pass     def appendleft(self, *args, **kwargs): # real signature unknown         """ Add an element to the left side of the deque. """         pass     def clear(self, *args, **kwargs): # real signature unknown         """ Remove all elements from the deque. """         pass     def count(self, value): # real signature unknown; restored from __doc__         """ D.count(value) -> integer -- return number of occurrences of value """         return 0     def extend(self, *args, **kwargs): # real signature unknown         """ Extend the right side of the deque with elements from the iterable """         pass     def extendleft(self, *args, **kwargs): # real signature unknown         """ Extend the left side of the deque with elements from the iterable """         pass     def pop(self, *args, **kwargs): # real signature unknown         """ Remove and return the rightmost element. """         pass     def popleft(self, *args, **kwargs): # real signature unknown         """ Remove and return the leftmost element. """         pass     def remove(self, value): # real signature unknown; restored from __doc__         """ D.remove(value) -- remove first occurrence of value. """         pass     def reverse(self): # real signature unknown; restored from __doc__         """ D.reverse() -- reverse *IN PLACE* """         pass     def rotate(self, *args, **kwargs): # real signature unknown         """ Rotate the deque n steps to the right (default n=1).  If n is negative, rotates left. """         pass     def __copy__(self, *args, **kwargs): # real signature unknown         """ Return a shallow copy of a deque. """         pass     def __delitem__(self, y): # real signature unknown; restored from __doc__         """ x.__delitem__(y) <==> del x[y] """         pass     def __eq__(self, y): # real signature unknown; restored from __doc__         """ x.__eq__(y) <==> x==y """         pass     def __getattribute__(self, name): # real signature unknown; restored from __doc__         """ x.__getattribute__('name') <==> x.name """         pass     def __getitem__(self, y): # real signature unknown; restored from __doc__         """ x.__getitem__(y) <==> x[y] """         pass     def __ge__(self, y): # real signature unknown; restored from __doc__         """ x.__ge__(y) <==> x>=y """         pass     def __gt__(self, y): # real signature unknown; restored from __doc__         """ x.__gt__(y) <==> x>y """         pass     def __iadd__(self, y): # real signature unknown; restored from __doc__         """ x.__iadd__(y) <==> x+=y """         pass     def __init__(self, iterable=(), maxlen=None): # known case of _collections.deque.__init__         """         deque([iterable[, maxlen]]) --> deque object         Build an ordered collection with optimized access from its endpoints.         # (copied from class doc)         """         pass     def __iter__(self): # real signature unknown; restored from __doc__         """ x.__iter__() <==> iter(x) """         pass     def __len__(self): # real signature unknown; restored from __doc__         """ x.__len__() <==> len(x) """         pass     def __le__(self, y): # real signature unknown; restored from __doc__         """ x.__le__(y) <==> x<=y """         pass     def __lt__(self, y): # real signature unknown; restored from __doc__         """ x.__lt__(y) <==> x<y """         pass     @staticmethod # known case of __new__     def __new__(S, *more): # real signature unknown; restored from __doc__         """ T.__new__(S, ...) -> a new object with type S, a subtype of T """         pass     def __ne__(self, y): # real signature unknown; restored from __doc__         """ x.__ne__(y) <==> x!=y """         pass     def __reduce__(self, *args, **kwargs): # real signature unknown         """ Return state information for pickling. """         pass     def __repr__(self): # real signature unknown; restored from __doc__         """ x.__repr__() <==> repr(x) """         pass     def __reversed__(self): # real signature unknown; restored from __doc__         """ D.__reversed__() -- return a reverse iterator over the deque """         pass     def __setitem__(self, i, y): # real signature unknown; restored from __doc__         """ x.__setitem__(i, y) <==> x[i]=y """         pass     def __sizeof__(self): # real signature unknown; restored from __doc__         """ D.__sizeof__() -- size of D in memory, in bytes """         pass     maxlen = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default     """maximum size of a deque or None if unbounded"""     __hash__ = None deque

deque

既然有双向队列,当然肯定得有标准的队列(先进先出 FIFO )

 class Queue:     """Create a queue object with a given maximum size.     If maxsize is <= 0, the queue size is infinite.     """     def __init__(self, maxsize=0):         self.maxsize = maxsize         self._init(maxsize)         # mutex must be held whenever the queue is mutating.  All methods         # that acquire mutex must release it before returning.  mutex         # is shared between the three conditions, so acquiring and         # releasing the conditions also acquires and releases mutex.         self.mutex = _threading.Lock()         # Notify not_empty whenever an item is added to the queue; a         # thread waiting to get is notified then.         self.not_empty = _threading.Condition(self.mutex)         # Notify not_full whenever an item is removed from the queue;         # a thread waiting to put is notified then.         self.not_full = _threading.Condition(self.mutex)         # Notify all_tasks_done whenever the number of unfinished tasks         # drops to zero; thread waiting to join() is notified to resume         self.all_tasks_done = _threading.Condition(self.mutex)         self.unfinished_tasks = 0     def task_done(self):         """Indicate that a formerly enqueued task is complete.         Used by Queue consumer threads.  For each get() used to fetch a task,         a subsequent call to task_done() tells the queue that the processing         on the task is complete.         If a join() is currently blocking, it will resume when all items         have been processed (meaning that a task_done() call was received         for every item that had been put() into the queue).         Raises a ValueError if called more times than there were items         placed in the queue.         """         self.all_tasks_done.acquire()         try:             unfinished = self.unfinished_tasks - 1             if unfinished <= 0:                 if unfinished < 0:                     raise ValueError('task_done() called too many times')                 self.all_tasks_done.notify_all()             self.unfinished_tasks = unfinished         finally:             self.all_tasks_done.release()     def join(self):         """Blocks until all items in the Queue have been gotten and processed.         The count of unfinished tasks goes up whenever an item is added to the         queue. The count goes down whenever a consumer thread calls task_done()         to indicate the item was retrieved and all work on it is complete.         When the count of unfinished tasks drops to zero, join() unblocks.         """         self.all_tasks_done.acquire()         try:             while self.unfinished_tasks:                 self.all_tasks_done.wait()         finally:             self.all_tasks_done.release()     def qsize(self):         """Return the approximate size of the queue (not reliable!)."""         self.mutex.acquire()         n = self._qsize()         self.mutex.release()         return n     def empty(self):         """Return True if the queue is empty, False otherwise (not reliable!)."""         self.mutex.acquire()         n = not self._qsize()         self.mutex.release()         return n     def full(self):         """Return True if the queue is full, False otherwise (not reliable!)."""         self.mutex.acquire()         n = 0 < self.maxsize == self._qsize()         self.mutex.release()         return n     def put(self, item, block=True, timeout=None):         """Put an item into the queue.         If optional args 'block' is true and 'timeout' is None (the default),         block if necessary until a free slot is available. If 'timeout' is         a non-negative number, it blocks at most 'timeout' seconds and raises         the Full exception if no free slot was available within that time.         Otherwise ('block' is false), put an item on the queue if a free slot         is immediately available, else raise the Full exception ('timeout'         is ignored in that case).         """         self.not_full.acquire()         try:             if self.maxsize > 0:                 if not block:                     if self._qsize() == self.maxsize:                         raise Full                 elif timeout is None:                     while self._qsize() == self.maxsize:                         self.not_full.wait()                 elif timeout < 0:                     raise ValueError("'timeout' must be a non-negative number")                 else:                     endtime = _time() + timeout                     while self._qsize() == self.maxsize:                         remaining = endtime - _time()                         if remaining <= 0.0:                             raise Full                         self.not_full.wait(remaining)             self._put(item)             self.unfinished_tasks += 1             self.not_empty.notify()         finally:             self.not_full.release()     def put_nowait(self, item):         """Put an item into the queue without blocking.         Only enqueue the item if a free slot is immediately available.         Otherwise raise the Full exception.         """         return self.put(item, False)     def get(self, block=True, timeout=None):         """Remove and return an item from the queue.         If optional args 'block' is true and 'timeout' is None (the default),         block if necessary until an item is available. If 'timeout' is         a non-negative number, it blocks at most 'timeout' seconds and raises         the Empty exception if no item was available within that time.         Otherwise ('block' is false), return an item if one is immediately         available, else raise the Empty exception ('timeout' is ignored         in that case).         """         self.not_empty.acquire()         try:             if not block:                 if not self._qsize():                     raise Empty             elif timeout is None:                 while not self._qsize():                     self.not_empty.wait()             elif timeout < 0:                 raise ValueError("'timeout' must be a non-negative number")             else:                 endtime = _time() + timeout                 while not self._qsize():                     remaining = endtime - _time()                     if remaining <= 0.0:                         raise Empty                     self.not_empty.wait(remaining)             item = self._get()             self.not_full.notify()             return item         finally:             self.not_empty.release()     def get_nowait(self):         """Remove and return an item from the queue without blocking.         Only get an item if one is immediately available. Otherwise         raise the Empty exception.         """         return self.get(False)     # Override these methods to implement other queue organizations     # (e.g. stack or priority queue).     # These will only be called with appropriate locks held     # Initialize the queue representation     def _init(self, maxsize):         self.queue = deque()     def _qsize(self, len=len):         return len(self.queue)     # Put a new item in the queue     def _put(self, item):         self.queue.append(item)     # Get an item from the queue     def _get(self):         return self.queue.popleft() Queue.Queue

Queue

二、subprocess

  如果你是做运维的,那么这个模块可能是用得最多的,它可以执行本地操作系统的相关命令。

  在python3之前可以执行shell命令的相关模块和函数有:

  • os.system
  • os.popen
  • os.spawn*
  • os.popen*          –废弃
  • popen2.*           –废弃
  • commands.*      –废弃,3.x中被移除

 python3以后以上执行shell命令的相关的模块和函数的功能均在 subprocess 模块中实现,并提供了更丰富的功能。 

call 

执行命令,返回状态码。经实际测试,shell=False这个参数已经不能用了,所以,无论你的命令字符串是怎么的格式,都必须使用shell=True这个参数.

另外,还有一个run方法,可以替代call,使用方法和效果是一样的。

ret = subprocess.call(["ls", "-l"], shell=False)  #已经废弃ret = subprocess.call("ls -l", shell=True)

check_call

执行命令,如果执行状态码是 0 ,则返回0,否则抛异常

import subprocessret = subprocess.check_call(["dir", "c:"], shell=True)print(ret)执行结果: C:\ ��Ŀ¼2015/12/13  12:31             3,488 bootsqm.dat2015/10/19  00:43    <DIR>          Drivers2015/11/02  19:43               164 log_followvideo.txt2016/06/05  22:05    <DIR>          PerfLogs2016/06/09  00:29    <DIR>          Program Files2016/06/21  10:38    <DIR>          Program Files (x86)2016/03/02  16:17    <DIR>          Python272016/05/20  09:56    <DIR>          Python352015/10/19  00:50    <DIR>          Users2016/06/15  17:08    <DIR>          Windows               2 ���ļ�          3,652 �ֽ�               8 ��Ŀ¼ 21,048,045,568 �����ֽ�0   #注意返回值是0,上面的是屏幕显示

  

check_output

执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常

import subprocessret = subprocess.check_output(["dir", "c:"], shell=True)print(ret)执行结果:b' \xc7\xfd\xb6\xaf\xc6\xf7 C \xd6\xd0\xb5\xc4\xbe\xed\xca\xc7 \xcf\xb5\xcd\xb3\r\n \xbe\xed\xb5\xc4\xd0\xf2\xc1\xd0\xba\xc5\xca\xc7 C038-3181\r\n\r\n C:\\ \xb5\xc4\xc4\xbf\xc2\xbc\r\n\r\n2015/12/13  12:31             3,488 bootsqm.dat\r\n2015/10/19  00:43    <DIR>          Drivers\r\n2015/11/02  19:43               164 log_followvideo.txt\r\n2016/06/05  22:05    <DIR>          PerfLogs\r\n2016/06/09  00:29    <DIR>          Program Files\r\n2016/06/21  10:38    <DIR>          Program Files (x86)\r\n2016/03/02  16:17    <DIR>          Python27\r\n2016/05/20  09:56    <DIR>          Python35\r\n2015/10/19  00:50    <DIR>          Users\r\n2016/06/15  17:08    <DIR>          Windows\r\n               2 \xb8\xf6\xce\xc4\xbc\xfe          3,652 \xd7\xd6\xbd\xda\r\n               8 \xb8\xf6\xc4\xbf\xc2\xbc 21,050,003,456 \xbf\xc9\xd3\xc3\xd7\xd6\xbd\xda\r\n'#返回结果是一个bytes类型的数据结构

 我们处理一下上面的返回结果:

import subprocessret = subprocess.check_output(["dir", "c:"], shell=True)ret = ret.decode("GBK")print(ret)运行结果:驱动器 C 中的卷是 系统 卷的序列号是 C038-3181 C:\ 的目录2015/12/13  12:31             3,488 bootsqm.dat2015/10/19  00:43    <DIR>          Drivers2015/11/02  19:43               164 log_followvideo.txt2016/06/05  22:05    <DIR>          PerfLogs2016/06/09  00:29    <DIR>          Program Files2016/06/21  10:38    <DIR>          Program Files (x86)2016/03/02  16:17    <DIR>          Python272016/05/20  09:56    <DIR>          Python352015/10/19  00:50    <DIR>          Users2016/06/15  17:08    <DIR>          Windows               2 个文件          3,652 字节               8 个目录 21,047,693,312 可用字节

subprocess.Popen(…)

用于执行复杂的系统命令。这个方法应该是subprocess模块里最核心的了。

参数:

    • args:shell命令,可以是字符串或者序列类型(如:list,元组)
    • bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
    • stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄    #最关键的参数
    • preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
    • close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
      所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
    • shell:同上。
    • cwd:用于设置子进程的当前目录
    • env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
    • universal_newlines:不同系统的换行符不同,True -> 同意使用 \n
    • startupinfo与createionflags只在windows下有效
      将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等
import subprocessret2 = subprocess.Popen("mkdir t2", shell=True)

由于终端中执行的命令有两种,一种是立刻显示结果的,一种是交互式环境的,在执行popen命令时要区别对待。

  • 输入即可得到输出,如:ifconfig
  • 输入进行某环境,依赖再输入,如:python
import subprocessobj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',)

注意下面例子中,subprocess.PIPE的用法,和stdin,stdout,stderr的配合。  

import subprocessobj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)obj.stdin.write("print(1)\n")obj.stdin.write("print(2)")obj.stdin.close()cmd_out = obj.stdout.read()obj.stdout.close()cmd_error = obj.stderr.read()obj.stderr.close()print(cmd_out)print(cmd_error)

注意下面例子中的communicate方法,它可以将stdout和stderr的信息都保存起来。

import subprocessobj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)obj.stdin.write("print(1)\n")obj.stdin.write("print(2)")out_error_list = obj.communicate()print(out_error_list)

下面的例子进一步简化了代码,直接将命令作为参数传递给了communicate方法。 

import subprocessobj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)out_error_list = obj.communicate('print("hello")')print(out_error_list)

三、configparser

做运维的同学对下面的文件格式一定不陌生:

[DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLevel = 9ForwardX11 = yes[bitbucket.org]User = hg[topsecret.server.com]Port = 50022ForwardX11 = no

configparser就是用于处理上面这种特定格式的文件,其本质上是利用open来操作文件。在python3以前,这个模块叫configParser,注意大小写。

将配置文件抽象一下:

# 注释1;  注释2[section1] # 节点k1 = v1    # 值k2:v2       # 值[section2] # 节点k1 = v1    # 值

如果想用python生成一个这样的文档怎么做呢?(注意其中的default这个section,它是个默认设置,相当于其他section都包含了它里面的内容)

import configparserconfig = configparser.ConfigParser()config["DEFAULT"] = {'ServerAliveInterval': '45',                      'Compression': 'yes',                     'CompressionLevel': '9'}config['bitbucket.org'] = {}config['bitbucket.org']['User'] = 'hg'config['topsecret.server.com'] = {}topsecret = config['topsecret.server.com']topsecret['Host Port'] = '50022'     # mutates the parsertopsecret['ForwardX11'] = 'no'  # same hereconfig['DEFAULT']['ForwardX11'] = 'yes'with open('example.ini', 'w') as configfile:   config.write(configfile)

写完了还可以再读出来!

 >>> import configparser >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] ' >>> for key in config['bitbucket.org']: print(key) ... user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'

viewcode

configparser提供了丰富的增删改查配置文件的方法:

1、获取所有节点: sections()

import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')ret = config.sections()print(ret)

2、获取指定节点下所有的键值对:items(“section”)

import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')ret = config.items('section1')print(ret

3、获取指定节点下所有的键:options(“section”)  

import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')ret = config.options('section1')print(ret)

4、获取指定节点下指定key的值:get(“section”,”key”)  

import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')v = config.get('section1', 'k1')# v = config.getint('section1', 'k1')# v = config.getfloat('section1', 'k1')# v = config.getboolean('section1', 'k1')print(v)

5、检查、删除、添加节点:

has_section(“section”)   add_section(“section”)    remove_section(“section”)

这些操作结束后都需要写入文件,保存状态,config.write(open(“file”,”w”)) 

import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')# 检查has_sec = config.has_section('section1')print(has_sec)# 添加节点config.add_section("SEC_1")config.write(open('xxxooo', 'w'))# 删除节点config.remove_section("SEC_1")config.write(open('xxxooo', 'w'))

6、检查、删除、设置指定组内的键值对:

has_option(“section”,”key”)    set(“section”,”key”,”value”)    remove_option(“section”,”key”)

注意:增加键值对不是add_option 而是set。

这些操作结束后都需要写入文件,保存状态,config.write(open(“file”,”w”))

import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')# 检查has_opt = config.has_option('section1', 'k1')print(has_opt)# 删除config.remove_option('section1', 'k1')config.write(open('xxxooo', 'w'))# 设置config.set('section1', 'k10', "123")config.write(open('xxxooo', 'w'))

四、XML

xml是一种不同语言或程序之间进行数据交换的协议,跟json差不多,比json古老,相比之下json使用起来更简单,因此逐渐被json替代。虽然如此,因为历史和架构的原因很多传统公司如金融行业的系统接口还主要是xml。

xml和html有点类似,都是通过<>节点来区别数据结构的,下面是一个例子:

 <data>     <country name="Liechtenstein">         <rank updated="yes">2</rank>         <year>2023</year>         <gdppc>141100</gdppc>         <neighbor direction="E" name="Austria" />         <neighbor direction="W" name="Switzerland" />     </country>     <country name="Singapore">         <rank updated="yes">5</rank>         <year>2026</year>         <gdppc>59900</gdppc>         <neighbor direction="N" name="Malaysia" />     </country>     <country name="Panama">         <rank updated="yes">69</rank>         <year>2026</year>         <gdppc>13600</gdppc>         <neighbor direction="W" name="Costa Rica" />         <neighbor direction="E" name="Colombia" />     </country> </data>

xml例子

由例子中可见,xml文件的每一个节点都可以有tag(标签名 )、attrib(节点属性)、text(节点内容)和子节点。最顶层的节点就是根节点。

所有的节点都被看做是一个Element类的实例。所有的操作也是以element元素为对象。

1.解析XML

有两种方式解析XML。

a :利用ElementTree.XML将字符串解析成xml对象

from xml.etree import ElementTree as ET# 打开文件,读取XML内容str_xml = open('xo.xml', 'r').read()# 将字符串解析成xml特殊对象,root代指xml文件的根节点root = ET.XML(str_xml)

b :利用ElementTree.parse将文件直接解析成xml对象

from xml.etree import ElementTree as ET# 直接解析xml文件tree = ET.parse("xo.xml")# 获取xml文件的根节点root = tree.getroot()

注意:在xml模块里,tree是一个很重要的类。上面两种方法读取xml文件后,在完成工作需要保存xml文件时都需要使用tree来保存。因此,a方法需要再生成一个tree对象,而b方法通过ET.parse直接就获得了一个tree对象,并通过该对象的getroot方法获得了xml文件的根节点。 

2、操作XML

XML格式类型是节点嵌套节点,对于每一个节点均有以下功能,以便对当前节点进行操作:

class Element:    """An XML element.    This class is the reference implementation of the Element interface.    An element's length is its number of subelements.  That means if you    want to check if an element is truly empty, you should check BOTH    its length AND its text attribute.    The element tag, attribute names, and attribute values can be either    bytes or strings.    *tag* is the element name.  *attrib* is an optional dictionary containing    element attributes. *extra* are additional element attributes given as    keyword arguments.    Example form:        <tag attrib>text<child/>...</tag>tail    """    当前节点的标签名    tag = None    """The element's name."""    当前节点的属性    attrib = None    """Dictionary of the element's attributes."""    当前节点的内容    text = None    """    Text before first subelement. This is either a string or the value None.    Note that if there is no text, this attribute may be either    None or the empty string, depending on the parser.    """    tail = None    """    Text after this element's end tag, but before the next sibling element's    start tag.  This is either a string or the value None.  Note that if there    was no text, this attribute may be either None or an empty string,    depending on the parser.    """    def __init__(self, tag, attrib={}, **extra):        if not isinstance(attrib, dict):            raise TypeError("attrib must be dict, not %s" % (                attrib.__class__.__name__,))        attrib = attrib.copy()        attrib.update(extra)        self.tag = tag        self.attrib = attrib        self._children = []    def __repr__(self):        return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self))    def makeelement(self, tag, attrib):        创建一个新节点        """Create a new element with the same type.        *tag* is a string containing the element name.        *attrib* is a dictionary containing the element attributes.        Do not call this method, use the SubElement factory function instead.        """        return self.__class__(tag, attrib)    def copy(self):        """Return copy of current element.        This creates a shallow copy. Subelements will be shared with the        original tree.        """        elem = self.makeelement(self.tag, self.attrib)        elem.text = self.text        elem.tail = self.tail        elem[:] = self        return elem    def __len__(self):        return len(self._children)    def __bool__(self):        warnings.warn(            "The behavior of this method will change in future versions.  "            "Use specific 'len(elem)' or 'elem is not None' test instead.",            FutureWarning, stacklevel=2            )        return len(self._children) != 0 # emulate old behaviour, for now    def __getitem__(self, index):        return self._children[index]    def __setitem__(self, index, element):        # if isinstance(index, slice):        #     for elt in element:        #         assert iselement(elt)        # else:        #     assert iselement(element)        self._children[index] = element    def __delitem__(self, index):        del self._children[index]    def append(self, subelement):        为当前节点追加一个子节点        """Add *subelement* to the end of this element.        The new element will appear in document order after the last existing        subelement (or directly after the text, if it's the first subelement),        but before the end tag for this element.        """        self._assert_is_element(subelement)        self._children.append(subelement)    def extend(self, elements):        为当前节点扩展 n 个子节点        """Append subelements from a sequence.        *elements* is a sequence with zero or more elements.        """        for element in elements:            self._assert_is_element(element)        self._children.extend(elements)    def insert(self, index, subelement):        在当前节点的子节点中插入某个节点,即:为当前节点创建子节点,然后插入指定位置        """Insert *subelement* at position *index*."""        self._assert_is_element(subelement)        self._children.insert(index, subelement)    def _assert_is_element(self, e):        # Need to refer to the actual Python implementation, not the        # shadowing C implementation.        if not isinstance(e, _Element_Py):            raise TypeError('expected an Element, not %s' % type(e).__name__)    def remove(self, subelement):        在当前节点在子节点中删除某个节点        """Remove matching subelement.        Unlike the find methods, this method compares elements based on        identity, NOT ON tag value or contents.  To remove subelements by        other means, the easiest way is to use a list comprehension to        select what elements to keep, and then use slice assignment to update        the parent element.        ValueError is raised if a matching element could not be found.        """        # assert iselement(element)        self._children.remove(subelement)    def getchildren(self):        获取所有的子节点(废弃)        """(Deprecated) Return all subelements.        Elements are returned in document order.        """        warnings.warn(            "This method will be removed in future versions.  "            "Use 'list(elem)' or iteration over elem instead.",            DeprecationWarning, stacklevel=2            )        return self._children    def find(self, path, namespaces=None):        获取第一个寻找到的子节点        """Find first matching element by tag name or path.        *path* is a string having either an element tag or an XPath,        *namespaces* is an optional mapping from namespace prefix to full name.        Return the first matching element, or None if no element was found.        """        return ElementPath.find(self, path, namespaces)    def findtext(self, path, default=None, namespaces=None):        获取第一个寻找到的子节点的内容        """Find text for first matching element by tag name or path.        *path* is a string having either an element tag or an XPath,        *default* is the value to return if the element was not found,        *namespaces* is an optional mapping from namespace prefix to full name.        Return text content of first matching element, or default value if        none was found.  Note that if an element is found having no text        content, the empty string is returned.        """        return ElementPath.findtext(self, path, default, namespaces)    def findall(self, path, namespaces=None):        获取所有的子节点        """Find all matching subelements by tag name or path.        *path* is a string having either an element tag or an XPath,        *namespaces* is an optional mapping from namespace prefix to full name.        Returns list containing all matching elements in document order.        """        return ElementPath.findall(self, path, namespaces)    def iterfind(self, path, namespaces=None):        获取所有指定的节点,并创建一个迭代器(可以被for循环)        """Find all matching subelements by tag name or path.        *path* is a string having either an element tag or an XPath,        *namespaces* is an optional mapping from namespace prefix to full name.        Return an iterable yielding all matching elements in document order.        """        return ElementPath.iterfind(self, path, namespaces)    def clear(self):        清空节点        """Reset element.        This function removes all subelements, clears all attributes, and sets        the text and tail attributes to None.        """        self.attrib.clear()        self._children = []        self.text = self.tail = None    def get(self, key, default=None):        获取当前节点的属性值        """Get element attribute.        Equivalent to attrib.get, but some implementations may handle this a        bit more efficiently.  *key* is what attribute to look for, and        *default* is what to return if the attribute was not found.        Returns a string containing the attribute value, or the default if        attribute was not found.        """        return self.attrib.get(key, default)    def set(self, key, value):        为当前节点设置属性值        """Set element attribute.        Equivalent to attrib[key] = value, but some implementations may handle        this a bit more efficiently.  *key* is what attribute to set, and        *value* is the attribute value to set it to.        """        self.attrib[key] = value    def keys(self):        获取当前节点的所有属性的 key        """Get list of attribute names.        Names are returned in an arbitrary order, just like an ordinary        Python dict.  Equivalent to attrib.keys()        """        return self.attrib.keys()    def items(self):        获取当前节点的所有属性值,每个属性都是一个键值对        """Get element attributes as a sequence.        The attributes are returned in arbitrary order.  Equivalent to        attrib.items().        Return a list of (name, value) tuples.        """        return self.attrib.items()    def iter(self, tag=None):        在当前节点的子孙中根据节点名称寻找所有指定的节点,并返回一个迭代器(可以被for循环)。        """Create tree iterator.        The iterator loops over the element and all subelements in document        order, returning all elements with a matching tag.        If the tree structure is modified during iteration, new or removed        elements may or may not be included.  To get a stable set, use the        list() function on the iterator, and loop over the resulting list.        *tag* is what tags to look for (default is to return all elements)        Return an iterator containing all the matching elements.        """        if tag == "*":            tag = None        if tag is None or self.tag == tag:            yield self        for e in self._children:            yield from e.iter(tag)    # compatibility    def getiterator(self, tag=None):        # Change for a DeprecationWarning in 1.4        warnings.warn(            "This method will be removed in future versions.  "            "Use 'elem.iter()' or 'list(elem.iter())' instead.",            PendingDeprecationWarning, stacklevel=2        )        return list(self.iter(tag))    def itertext(self):        在当前节点的子孙中根据节点名称寻找所有指定的节点的内容,并返回一个迭代器(可以被for循环)。        """Create text iterator.        The iterator loops over the element and all subelements in document        order, returning all inner text.        """        tag = self.tag        if not isinstance(tag, str) and tag is not None:            return        if self.text:            yield self.text        for e in self:            yield from e.itertext()            if e.tail:                yield e.tail

节点功能一览表

由于 每个节点 都具有以上的方法,并且在上一步骤中解析时均得到了root(xml文件的根节点),因此可以利用以上方法进行操作xml文件。

a. 遍历XML文档的所有内容

from xml.etree import ElementTree as ET############ 解析方式一 ############"""# 打开文件,读取XML内容str_xml = open('xo.xml', 'r').read()# 将字符串解析成xml特殊对象,root代指xml文件的根节点root = ET.XML(str_xml)"""############ 解析方式二 ############# 直接解析xml文件tree = ET.parse("xo.xml")# 获取xml文件的根节点root = tree.getroot()### 操作# 顶层标签print(root.tag)# 遍历XML文档的第二层for child in root:    # 第二层节点的标签名称和标签属性    print(child.tag, child.attrib)    # 遍历XML文档的第三层    for i in child:        # 第二层节点的标签名称和内容        print(i.tag,i.text)

b、遍历XML中指定的节点

from xml.etree import ElementTree as ET############ 解析方式一 ############"""# 打开文件,读取XML内容str_xml = open('xo.xml', 'r').read()# 将字符串解析成xml特殊对象,root代指xml文件的根节点root = ET.XML(str_xml)"""############ 解析方式二 ############# 直接解析xml文件tree = ET.parse("xo.xml")# 获取xml文件的根节点root = tree.getroot()### 操作# 顶层标签print(root.tag)# 遍历XML中所有的year节点for node in root.iter('year'):    # 节点的标签名称和内容    print(node.tag, node.text)

c、修改节点内容

由于修改的节点时,均是在内存中进行,不会影响文件中的内容。所以,如果想要保存修改操作,则需要重新将内存中的内容写到文件。

下面是解析文件方式,直接获得了tree,因此可以直接修改文件并保存。

from xml.etree import ElementTree as ET############ 解析方式二 ############# 直接解析xml文件tree = ET.parse("xo.xml")# 获取xml文件的根节点root = tree.getroot()############ 操作 ############# 顶层标签print(root.tag)# 循环所有的year节点for node in root.iter('year'):    # 将year节点中的内容自增一    new_year = int(node.text) + 1    node.text = str(new_year)    # 设置属性    node.set('name', 'alex')    node.set(')    # 删除属性    del node.attrib['name']############ 保存文件 ############tree.write("newnew.xml", encoding='utf-8')解析文件方式,修改,保存

下面是解析字符串方式,由于没有获得tree,因此在保存的时候需要实例化一个tree对象。注意观察代码的最后两行。

from xml.etree import ElementTree as ET############ 解析方式一 ############# 打开文件,读取XML内容str_xml = open('xo.xml', 'r').read()# 将字符串解析成xml特殊对象,root代指xml文件的根节点root = ET.XML(str_xml)############ 操作 ############# 顶层标签print(root.tag)# 循环所有的year节点for node in root.iter('year'):    # 将year节点中的内容自增一    new_year = int(node.text) + 1    node.text = str(new_year)    # 设置属性    node.set('name', 'alex')    node.set(')    # 删除属性    del node.attrib['name']############ 保存文件 ############tree = ET.ElementTree(root)tree.write("newnew.xml", encoding='utf-8')解析字符串方式,修改,保存

d、删除节点

from xml.etree import ElementTree as ET############ 解析字符串方式打开 ############# 打开文件,读取XML内容str_xml = open('xo.xml', 'r').read()# 将字符串解析成xml特殊对象,root代指xml文件的根节点root = ET.XML(str_xml)############ 操作 ############# 顶层标签print(root.tag)# 遍历data下的所有country节点for country in root.findall('country'):    # 获取每一个country节点下rank节点的内容    rank = int(country.find('rank').text)    if rank > 50:        # 删除指定country节点        root.remove(country)############ 保存文件 ############tree = ET.ElementTree(root)tree.write("newnew.xml", encoding='utf-8')

解析字符串方式打开,删除,保存

from xml.etree import ElementTree as ET############ 解析文件方式 ############# 直接解析xml文件tree = ET.parse("xo.xml")# 获取xml文件的根节点root = tree.getroot()############ 操作 ############# 顶层标签print(root.tag)# 遍历data下的所有country节点for country in root.findall('country'):    # 获取每一个country节点下rank节点的内容    rank = int(country.find('rank').text)    if rank > 50:        # 删除指定country节点        root.remove(country)############ 保存文件 ############tree.write("newnew.xml", encoding='utf-8')

解析文件方式打开,删除,保存

3、创建XML文档

from xml.etree import ElementTree as ET# 创建根节点root = ET.Element("famliy")# 创建节点大儿子son1 = ET.Element('son', {'name': '儿1'})# 创建小儿子son2 = ET.Element('son', {"name": '儿2'})# 在大儿子中创建两个孙子grandson1 = ET.Element('grandson', {'name': '儿11'})grandson2 = ET.Element('grandson', {'name': '儿12'})son1.append(grandson1)son1.append(grandson2)# 把儿子添加到根节点中root.append(son1)root.append(son1)tree = ET.ElementTree(root)tree.write('oooo.xml',encoding='utf-8', short_empty_elements=False)

创建方式(一)

from xml.etree import ElementTree as ET# 创建根节点root = ET.Element("famliy")# 创建大儿子# son1 = ET.Element('son', {'name': '儿1'})son1 = root.makeelement('son', {'name': '儿1'})# 创建小儿子# son2 = ET.Element('son', {"name": '儿2'})son2 = root.makeelement('son', {"name": '儿2'})# 在大儿子中创建两个孙子# grandson1 = ET.Element('grandson', {'name': '儿11'})grandson1 = son1.makeelement('grandson', {'name': '儿11'})# grandson2 = ET.Element('grandson', {'name': '儿12'})grandson2 = son1.makeelement('grandson', {'name': '儿12'})son1.append(grandson1)son1.append(grandson2)# 把儿子添加到根节点中root.append(son1)root.append(son1)tree = ET.ElementTree(root)tree.write('oooo.xml',encoding='utf-8', short_empty_elements=False)

创建方式(二)

from xml.etree import ElementTree as ET# 创建根节点root = ET.Element("famliy")# 创建节点大儿子son1 = ET.SubElement(root, "son", attrib={'name': '儿1'})# 创建小儿子son2 = ET.SubElement(root, "son", attrib={"name": "儿2"})# 在大儿子中创建一个孙子grandson1 = ET.SubElement(son1, "age", attrib={'name': '儿11'})grandson1.text = '孙子'et = ET.ElementTree(root)  #生成文档对象et.write("test.xml", encoding="utf-8", xml_declaration=True, short_empty_elements=False)

创建方式(三)

由于原生保存的XML时默认无缩进,如果想要设置缩进的话, 需要修改保存方式:

from xml.etree import ElementTree as ETfrom xml.dom import minidomdef prettify(elem):    """将节点转换成字符串,并添加缩进。    """    rough_string = ET.tostring(elem, 'utf-8')    reparsed = minidom.parseString(rough_string)    return reparsed.toprettyxml(indent="\t")# 创建根节点root = ET.Element("famliy")# 创建大儿子# son1 = ET.Element('son', {'name': '儿1'})son1 = root.makeelement('son', {'name': '儿1'})# 创建小儿子# son2 = ET.Element('son', {"name": '儿2'})son2 = root.makeelement('son', {"name": '儿2'})# 在大儿子中创建两个孙子# grandson1 = ET.Element('grandson', {'name': '儿11'})grandson1 = son1.makeelement('grandson', {'name': '儿11'})# grandson2 = ET.Element('grandson', {'name': '儿12'})grandson2 = son1.makeelement('grandson', {'name': '儿12'})son1.append(grandson1)son1.append(grandson2)# 把儿子添加到根节点中root.append(son1)root.append(son1)raw_str = prettify(root)f = open("xxxoo.xml",'w',encoding='utf-8')f.write(raw_str)f.close()

自动缩进

4、命名空间

from xml.etree import ElementTree as ETET.register_namespace('com',"http://www.company.com") #some name# build a tree structureroot = ET.Element("{http://www.company.com}STUFF")body = ET.SubElement(root, "})body.text = "STUFF EVERYWHERE!"# wrap it in an ElementTree instance, and save as XMLtree = ET.ElementTree(root)tree.write("page.xml",           xml_declaration=True,           encoding='utf-8',           method="xml")命名空间

五、shutil

  shutil模块是python为我们封装的一个高级的高级的文件、文件夹、压缩包 处理模块,它本质上是调用open方法对文件进行读写。模块相对比较简单,记住几个常用的方法即可。

1、shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另一个文件中。这个是基本方法,其他的拷贝方法都是在后台调用这个方法。 

import shutilshutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))

2、shutil.copyfile(src, dst)
拷贝文件 

shutil.copyfile('f1.log', 'f2.log')

3、shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变 

shutil.copymode('f1.log', 'f2.log')

4、shutil.copystat(src, dst)
仅拷贝状态的信息,包括:mode bits, atime, mtime, flags

shutil.copystat('f1.log', 'f2.log')

5、 shutil.copy(src, dst)
拷贝文件和权限

shutil.copy('f1.log', 'f2.log')

6、shutil.copy2(src, dst)
拷贝文件和状态信息

shutil.copy2('f1.log', 'f2.log')

7、shutil.ignore_patterns(*patterns)

shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件夹。ignor_patterns是指忽略不拷贝的文件

shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))

8、shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件

shutil.rmtree('folder1')

9、shutil.move(src, dst)
递归的去移动文件,它类似mv命令,其实就是重命名。 

shutil.move('folder1', 'folder3')

10、shutil.make_archive(base_name, format,…)

创建压缩包并返回文件路径,例如:zip、tar

创建压缩包并返回文件路径,例如:zip、tar

    • base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
      如:www                        =>保存至当前路径
      如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
    • format:压缩包种类,“zip”, “tar”, “bztar”,“gztar”
    • root_dir:要压缩的文件夹路径(默认当前目录)
    • owner:用户,默认当前用户
    • group:组,默认当前组
    • logger:用于记录日志,通常是logging.Logger对象
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录import shutilret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')#将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录import shutilret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')

  shutil 对压缩包的处理本质上是调用 ZipFile 和 TarFile 两个模块来进行的,但封装的比较简单,不是很好用,建议还是使用ZipFile 和 TarFile 模块。

  

  

 

  

  

  

  

 

 

  

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,154
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,623
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,465
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,239
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,874
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,042