[Python筆記]第八篇:模塊

 本篇主要內容:python經常使用模塊用法介紹node

什麼是模塊

  模塊,用一大段代碼實現了某個功能的代碼集合。 python

  相似於函數式編程和麪向過程編程,函數式編程則完成一個功能,其餘代碼用來調用便可,提供了代碼的重用性和代碼間的耦合。而對於一個複雜的功能來,可能須要多個函數才能完成(函數又能夠在不一樣的.py文件中),n個 .py 文件組成的代碼集合就稱爲模塊。mysql

如:os 是系統相關的模塊;file是文件操做相關的模塊linux

模塊分類:nginx

  • 自定義模塊
  • 開源模塊
  • 內置模塊

 

模塊導入

則能夠採用這幾種方式導入模塊,這些方法通用於導入自定義模塊/開源模塊/內置模塊正則表達式

import module
from module.xx.xx import xx
from module.xx.xx import xx as rename 
from module.xx.xx import *

 

模塊導入舉例

  下面介紹如何導入一個自定義模塊:redis

  如圖所示下面這個名爲"cnblogs"的項目裏面有一個文件夾叫作"libs"和一個叫作"index"的py文件算法

假若有個叫index.py的主程序,須要調用libs文件夾裏面的功能的話,那麼他就要在主程序裏import libs裏面存在的各個功能模塊sql

from libs import db
from libs import storage

  

 

#!/usr/bin/env python
#-*- coding: utf-8 -*-

def mysql(ns):
    print("mysql host is %s" %ns)

def oracle(ns):
    print("oracle host is %s" %ns)

def redis(ns):
    print("redis host is %s" %ns)

def mogodb(ns):
    print("mogodb host is %s" %ns)
db.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-

def node_us(ns):
    print("node1 host is %s" %ns)

def node_eu(ns):
    print("node2 host is %s" %ns)

def node_au(ns):
    print("node3 host is %s" %ns)
storage.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from libs import db
from libs import storage

db1 = db.mogodb("172.0.0.100")

db2 = db.redis("172.0.0.101")

s1 = storage.node_eu("10.10.10.1")

s2 = storage.node_us("10.10.10.3")

# 運行結果
"""
C:\Python35\python3.exe C:/Users/cnblogs/index.py
mogodb host is 172.0.0.100
redis host is 172.0.0.101
node2 host is 10.10.10.1
node1 host is 10.10.10.3

Process finished with exit code 0
"""
index.py

 

新增模塊路徑

  那麼問題來了,導入模塊時是根據那個路徑做爲基準來進行的呢?即:sys.pathshell

import sys
print(sys.path)

結果:
['C:\\Python35\\python35.zip', 'C:\\Python35\\DLLs', 'C:\\Python35\\lib', 'C:\\Python35', 'C:\\Python35\\lib\\site-packages']

 若是sys.path路徑列表沒有你想要的路徑,能夠經過 sys.path.append('路徑') 添加。 

import sys
import os
project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(project_path)

 

# 配置文件裏面
settings.py


import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))   # .

HOME_DIR = os.path.join(BASE_DIR, "home")

BIN_DIR = os.path.join(BASE_DIR, "bin")

LIB_DIR = os.path.join(BASE_DIR, "lib")

LOG_DIR = os.path.join(BASE_DIR, "log")


# 要引用配置的地方
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings


settings.HOME_DIR   # 調用"配置文件"
配置文件實例

 

開源模塊安裝

要使用開源模塊則須要提早安裝,python提供了兩種安裝方式:

源碼安裝

下載源碼
linux用終端;Windows用cmd切換到源碼所屬路徑
python setup.py

  

包管理工具安裝 

python3 -m pip install module 

  

內置模塊

內置模塊是Python自帶的功能,在使用內置模塊相應的功能時,須要【先導入】再【使用】

1.sys

  用於提供對Python解釋器相關的操做:

sys.argv           命令行參數List,第一個元素是程序自己路徑
sys.exit(n)        退出程序,正常退出時exit(0)
sys.version        獲取Python解釋程序的版本信息
sys.maxint         最大的Int值
sys.path           返回模塊的搜索路徑,初始化時使用PYTHONPATH環境變量的值
sys.platform       返回操做系統平臺名稱
sys.stdin          輸入相關
sys.stdout         輸出相關
sys.stderror       錯誤相關

  

'''百分比進度條'''
import sys
import time

for i in range(101):
    sys.stdout.write('\r') # 換行符
    sys.stdout.write('%s %%|%s' % (int(i / 100 * 100), int(i / 100 * 100) * ''))
    sys.stdout.flush()    #打印一次清空一次
    time.sleep(0.05)
sys模塊打印進度條

 

2.os

用於提供系統級別的操做:

os.getcwd()                 獲取當前工做目錄,即當前python腳本工做的目錄路徑
os.chdir("dirname")         改變當前腳本工做目錄;至關於shell下cd
os.curdir                   返回當前目錄: ('.')
os.pardir                   獲取當前目錄的父目錄字符串名:('..')
os.makedirs('dir1/dir2')    可生成多層遞歸目錄
os.removedirs('dirname1')   若目錄爲空,則刪除,並遞歸到上一級目錄,如若也爲空,則刪除,依此類推
os.mkdir('dirname')         生成單級目錄;至關於shell中mkdir dirname
os.rmdir('dirname')         刪除單級空目錄,若目錄不爲空則沒法刪除,報錯;至關於shell中rmdir dirname
os.listdir('dirname')       列出指定目錄下的全部文件和子目錄,包括隱藏文件,並以列表方式打印
os.remove()                 刪除一個文件
os.rename("oldname","new")  重命名文件/目錄
os.stat('path/filename')    獲取文件/目錄信息
os.sep                      操做系統特定的路徑分隔符,win下爲"\\",Linux下爲"/"
os.linesep                  當前平臺使用的行終止符,win下爲"\t\n",Linux下爲"\n"
os.pathsep                  用於分割文件路徑的字符串
os.name                     字符串指示當前使用平臺。win->'nt'; Linux->'posix'
os.system("bash command")   運行shell命令,直接顯示
os.environ                  獲取系統環境變量
os.path.abspath(path)       返回path規範化的絕對路徑
os.path.split(path)         將path分割成目錄和文件名二元組返回
os.path.dirname(path)       返回path的目錄。其實就是os.path.split(path)的第一個元素
os.path.basename(path)      返回path最後的文件名。如何path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素
os.path.exists(path)        若是path存在,返回True;若是path不存在,返回False
os.path.isabs(path)         若是path是絕對路徑,返回True
os.path.isfile(path)        若是path是一個存在的文件,返回True。不然返回False
os.path.isdir(path)         若是path是一個存在的目錄,則返回True。不然返回False
os.path.join(path1[, path2[, ...]])  將多個路徑組合後返回,第一個絕對路徑以前的參數將被忽略
os.path.getatime(path)      返回path所指向的文件或者目錄的最後存取時間
os.path.getmtime(path)      返回path所指向的文件或者目錄的最後修改時間

  

3.hashlib

用於加密相關的操做,代替了md5模塊和sha模塊,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

import hashlib
 
# ######## md5 ########
hash = hashlib.md5()
# help(hash.update)
hash.update(bytes('admin', encoding='utf-8'))
print(hash.hexdigest())
print(hash.digest())
 
 
######## sha1 ########
 
hash = hashlib.sha1()
hash.update(bytes('admin', encoding='utf-8'))
print(hash.hexdigest())
 
# ######## sha256 ########
 
hash = hashlib.sha256()
hash.update(bytes('admin', encoding='utf-8'))
print(hash.hexdigest())
 
 
# ######## sha384 ########
 
hash = hashlib.sha384()
hash.update(bytes('admin', encoding='utf-8'))
print(hash.hexdigest())
 
# ######## sha512 ########
 
hash = hashlib.sha512()
hash.update(bytes('admin', encoding='utf-8'))
print(hash.hexdigest())

  

以上加密算法雖然依然很是厲害,但時候存在缺陷,即:經過撞庫能夠反解。因此,有必要對加密算法中添加自定義key再來作加密。

import hashlib
 
# ######## md5 ########
 
hash = hashlib.md5(bytes('898oaFs09f',encoding="utf-8"))
hash.update(bytes('admin',encoding="utf-8"))
print(hash.hexdigest())

  

python內置還有一個 hmac 模塊,它內部對咱們建立 key 和 內容 進行進一步的處理而後再加密

import hmac
 
h = hmac.new(bytes('898oaFs09f',encoding="utf-8"))
h.update(bytes('admin',encoding="utf-8"))
print(h.hexdigest())

  

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import hashlib

def md5_hash(arg):
    ooo = hashlib.md5(bytes('cnblogs', encoding='utf-8'))  # 建立一個加鹽的加密對象
    ooo.update(bytes(arg, encoding='utf-8'))                # 加密字符串"123"
    return ooo.hexdigest()

def login(user, pwd):
    with open("db", "r", encoding="utf-8") as f:
        for line in f:
            u, p = line.strip().split(":")
            if u == user and p == md5_hash(pwd):
                return True

def register(user, pwd):
    with open("db", "a", encoding="utf-8") as f:
        temp = user + ":" + md5_hash(pwd)
        f.write(temp)

i = input("1,登陸; 2,註冊")

if i == "1":
    user = input("用戶名:")
    pwd = input("密碼:")
    r = login(user, pwd)
    if r:
        print("登陸成功")

    else:
        print("登陸失敗")

elif i == "2":
    user = input("用戶名:")
    pwd = input("密碼:")
    register(user, pwd)
hash驗證登陸demo

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
import sys


def md5sum(filename):
    md5 = hashlib.md5()
    with open(filename, 'rb') as f:
        for chunk in iter(lambda: f.read(8192), b''):
            md5.update(chunk)
    return md5.hexdigest()

if __name__ == "__main__":
    file_md5 = md5sum(sys.argv[1])
    print(file_md5)
md5sum文件校驗

 

4.random

import random
 
print(random.random())
print(random.randint(1, 2))
print(random.randrange(1, 10))

  

import random

temp = ""
for i in range(6):
    num = random.randrange(0,4)
    if num == 3 or num == 1:
        rad1 = random.randrange(1,11)
        temp += str(rad1)
    else:
        rad2 = random.randrange(65,91)
        c2 = chr(rad2)
        temp += c2

print(temp)
隨機驗證碼

  

 5.re模塊

請移步第九篇:re正則表達式

 

6.序列化

Python中用於序列化的兩個模塊

  • json     用於【字符串】和 【python基本數據類型】 間進行轉換
  • pickle   用於【python特有的類型】 和 【python基本數據類型】間進行轉換

 

json模塊提供了四個功能:dumps、dump、loads、load

dump   序列化python數據類型對象爲JOSN格式的類文件對象,dump 列表-> 字符串 -> 類文件對象
load     從josn類文件對象中反序列爲PYTHON數據類型對象, 打開文件->讀取內容-> python數據類型如(列表、字典)

dumps  序列化python數據類型對象爲字符串格式的josn對象,python數據類型如(列表、字典)->JOSN字符串
loads  將josn字符串反序列化爲PYTHON數據類型,josn字符-> python數據類型如(列表、字典)


pickle模塊提供了四個功能:dumps、dump、loads、load

pickle.dump(obj,file)   直接把對象序列化後寫入文件
pickle.load(file)           從文件中反序列化出對象

pickle.dumps(obj)           把任意對象序列化成一個str,而後,把這個str寫入文件
pickle.loads(string)      反序列化出對象

 

用json處理一個字典

dumps

#!/usr/bin/env python
#-*- coding: utf-8 -*-
import json

accounts_dic = {
    1000: {
        'name':'Da Wang',
        'email': 'jinjiaodawang@126.com',
        'passwd': '123456',
        'balance': 15000,
        'phone': 13800138000
    }
}
# 使用json.dumps序列化一個字典
accounts_json = json.dumps(accounts_dic)
print(accounts_json, type(accounts_json))

# 使用json.loads反序列化一個json
accounts_dic_new = json.loads(accounts_json)
print(accounts_dic_new, type(accounts_dic_new))

 

dump 

import json
user_dic = {
    "name": "Judy",
    "age": 21,
    "gender": "female"
}
json.dump(user_dic, open('userinfo.json', 'w'))   # 序列化字典到文件
new_dic = json.load(open('userinfo.json', 'r'))   # 從文件反序列化字典

  

 

用pickle處理一個字典並寫回文件

#!/usr/bin/env python
#-*- coding: utf-8 -*-
import pickle

accounts = {
    1000: {
        'name':'Da Wang',
        'email': 'jinjiaodawang@126.com',
        'passwd': '123456',
        'balance': 15000,
        'phone': 13800138000
    },
    1001: {
        'name': 'Xiao Guo',
        'email': 'xguo@126.com',
        'passwd': '123123',
        'balance': -15000,
        'phone': 13401760101
    },
}

f = open('account.db','wb')
f.write(pickle.dumps(accounts))
f.closed

 

7.xml

XML是實現不一樣語言或程序之間進行數據交換的協議,XML文件格式以下:

<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

 

from xml.etree import ElementTree as ET


# 打開文件,讀取XML內容
str_xml = open('xo.xml', 'r').read()

# 將字符串解析成xml特殊對象,root代指xml文件的根節點
root = ET.XML(str_xml)

利用ElementTree.XML將字符串解析成xml對象

 

from xml.etree import ElementTree as ET

# 直接解析xml文件
tree = ET.parse("xo.xml")

# 獲取xml文件的根節點
root = tree.getroot()

利用ElementTree.parse將文件直接解析成xml對象

 

操做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文件的根節點),so   能夠利用以上方法進行操做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、修改節點內容

因爲修改的節點時,均是在內存中進行,其不會影響文件中的內容。因此,若是想要修改,則須要從新將內存中的內容寫到文件。

 

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('age', '18')
    # 刪除屬性
    del node.attrib['name']


############ 保存文件 ############
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)

# 循環全部的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('age', '18')
    # 刪除屬性
    del node.attrib['name']


############ 保存文件 ############
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')

解析文件方式打開,刪除,保存

 

建立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時默認無縮進,若是想要設置縮進的話, 須要修改保存方式(從 xml.dom 導入 minidom):

from xml.etree import ElementTree as ET
from xml.dom import minidom


def 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()

 

 

xml命名空間

命名空間是爲了解決在 XML 中元素名稱命名衝突的狀況

詳細請看這裏

from xml.etree import ElementTree as ET

ET.register_namespace('com',"http://www.company.com") #some name

# build a tree structure
root = ET.Element("{http://www.company.com}STUFF")
body = ET.SubElement(root, "{http://www.company.com}MORE_STUFF", attrib={"{http://www.company.com}hhh": "123"})
body.text = "STUFF EVERYWHERE!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)

tree.write("page.xml",
           xml_declaration=True,
           encoding='utf-8',
           method="xml")

命名空間

 

8.time

時間相關的操做,時間有三種表示方式:

  • 時間戳               1970年1月1日以後的秒,即:time.time()
  • 格式化的字符串    2014-11-11 11:11,    即:time.strftime('%Y-%m-%d')
  • 結構化時間          元組包含了:年、日、星期等... time.struct_time    即:time.localtime()
print time.time()
print time.mktime(time.localtime())
  
print time.gmtime()    #可加時間戳參數
print time.localtime() #可加時間戳參數
print time.strptime('2014-11-11', '%Y-%m-%d')
  
print time.strftime('%Y-%m-%d') #默認當前時間
print time.strftime('%Y-%m-%d',time.localtime()) #默認當前時間
print time.asctime()
print time.asctime(time.localtime())
print time.ctime(time.time())
  
import datetime
'''
datetime.date:表示日期的類。經常使用的屬性有year, month, day
datetime.time:表示時間的類。經常使用的屬性有hour, minute, second, microsecond
datetime.datetime:表示日期時間
datetime.timedelta:表示時間間隔,即兩個時間點之間的長度
timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
strftime("%Y-%m-%d")
'''
import datetime
print datetime.datetime.now()
print datetime.datetime.now() - datetime.timedelta(days=5)

  

時間對象

 

time格式互轉圖示 

configparser

configparser用於處理特定格式的文件,其本質上是利用open來操做文件。

 

[nginx]
db = "mysql"

[httpd]
ip = "10.10.10.1"
db = "oracle"

[iis]
ip = 172.0.0.22

# 要處理的文件格式

 

#!/usr/bin/env python
#-*- coding: utf-8 -*-
import configparser

con = configparser.ConfigParser()
# con對象的read功能, 打開文件, 讀取文件, 放入內存
con.read('config', encoding='utf-8')
# con對象的sections, 內存中尋找全部的節點
result = con.sections()
print(result)

# 獲取指定節點下面的鍵值對
result1 = con.items('httpd')
print(result1)

# 獲取指定節點下面的全部鍵
ret = con.options("nginx")
print(ret)

# 獲取指定節點下的指定value值
result2 = con.get('nginx', 'ip')
print(result2)



# 檢查節點是否存在
print(con.has_section('nginx'))



# **********************************
# 添加刪除節點
# 添加節點
# con.add_section('iis')
# con.write(open('config', 'w'))

# 刪除節點
# con.remove_section('iis')
# con.write(open('config', 'w'))



# **********************************
# 檢查 刪除 設置指定節點內的鍵值對
# 檢查
print(con.has_option('nginx', 'ip'))

# 刪除
con.remove_option('nginx', 'ip')
# con.write(open('config', 'w'))

# 設置
con.set('iis', 'ip', '172.0.0.22')
con.write(open('config', 'w'))
相關文章
相關標籤/搜索