Python系列之模塊、和字符串格式化


Python 模塊

模塊讓你能夠有邏輯地組織你的Python代碼段。

把相關的代碼分配到一個 模塊里能讓你的代碼更好用,更易懂。

模塊也是Python對象,具有隨機的名字屬性用來綁定或引用。

模塊分為三種:

  • 自定義模塊 :自己編寫的模塊通過
  • 內置模塊 :又稱為標准庫,安裝Python之后自帶的模塊
  • 開源模塊:別人寫的模塊,需要安裝之后才可以使用

模塊的導入方法:

import module            #適用與內置模塊或使用模塊的代碼文件(A)與模塊文件(B)在同一個目錄
from xxx.xxx.xxx.module import xx        #適用於模塊在別的目錄下,在導入時使用 . (點)作為目錄的分割
from xxx.xxx.xxx,module import xx as rename    #適用於所需要調用的模塊比較長或者難記,這個辦法是給模塊起一個別名
from xxx.xxx.xxx.module import *         #適用於把目錄下的所有模塊都導入

導入模塊其實就是告訴Python解釋器去解釋那個py文件

  • 導入一個py文件,解釋器解釋該py文件
  • 導入一個包,解釋器解釋該包下的 __init__.py 文件

sys.path添加目錄

如果sys.path路徑列表沒有你想要的路徑,可以通過 sys.path.append('路徑') 添加。
通過os模塊可以獲取各種目錄;

 

 內置模塊

1、os模塊,提供系統級別的操作

os.getcwd() 獲取當前工作目錄,即當前python腳本工作的目錄路徑
os.chdir("dirname")  改變當前腳本工作目錄;相當於shell下cd
os.curdir  返回當前目錄: ('.')
os.pardir  獲取當前目錄的父目錄字符串名:('..')
os.makedirs('dirname1/dirname2')    可生成多層遞歸目錄
os.removedirs('dirname1')    若目錄為空,則刪除,並遞歸到上一級目錄,如若也為空,則刪除,依此類推
os.mkdir('dirname')    生成單級目錄;相當於shell中mkdir dirname
os.rmdir('dirname')    刪除單級空目錄,若目錄不為空則無法刪除,報錯;相當於shell中rmdir dirname
os.listdir('dirname')    列出指定目錄下的所有文件和子目錄,包括隱藏文件,並以列表方式打印
os.remove()  刪除一個文件
os.rename("oldname","newname")  重命名文件/目錄
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所指向的文件或者目錄的最后修改時間
View Code

 2、sys模塊,用於提供對解釋器相關的操作

sys.argv: 實現從程序外部向程序傳遞參數。
sys.exit([arg]): 程序中間的退出,arg=0為正常退出。
sys.getdefaultencoding(): 獲取系統當前編碼,一般默認為ascii。
sys.setdefaultencoding(): 設置系統默認編碼,執行dir(sys)時不會看到這個方法,在解釋器中執行不通過,可以先執行reload(sys),在執行 setdefaultencoding('utf8'),此時將系統默認編碼設置為utf8。(見設置系統默認編碼 )
sys.getfilesystemencoding(): 獲取文件系統使用編碼方式,Windows下返回'mbcs',mac下返回'utf-8'.
sys.path: 獲取指定模塊搜索路徑的字符串集合,可以將寫好的模塊放在得到的某個路徑下,就可以在程序中import時正確找到。
sys.platform: 獲取當前系統平台。
sys.stdin,sys.stdout,sys.stderr stdin , stdout , 以及stderr 變量包含與標准I/O 流對應的流對象. 如果需要更好地控制輸出,而print 不能滿足你的要求, 它們就是你所需要的. 你也可以替換它們, 這時候你就可以重定向輸出和輸入到其它設備( device ), 或者以非標准的方式處理它們
sys Code

3、time模塊,提供系統時間的操作

print(time.time())  #返回當前系統時間戳
print(time.ctime()) #Thu May 19 18:14:45 2016 當前系統時間
print(time.ctime(time.time())) # 將時間戳轉換為字符串格式時間-->Thu May 19 18:16:18 2016
print(time.gmtime()) #打印struct_time格式時間
print(time.localtime()) #以struct_time格式顯示本地時間
print(time.mktime(time.gmtime())) #將struct_time格式時間轉換為時間戳格式
print(time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime())) #將struct_time格式轉換為指定格式
print(time.strptime('2016-05-19','%Y-%m-%d')) # 將字符串格式轉換為struct_time格式
time Code

4、datetime模塊,提供date與time的結合體包括date與time的所有信息

print(datetime.date.today()) #輸出格式2016-05-21
print(datetime.date.fromtimestamp(time.time() - 86400)) #將時間戳轉換成日期格式 86400代表一天
cur_time = datetime.datetime.now()
print(cur_time) #2016-05-21 20:06:38.319101
print(cur_time.timetuple()) #返回struct_time 格式
print(cur_time.replace(2016,5,12)) #2016-05-12 20:07:53.104379
datetime Code

5、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())
View Code

使用new()創建指定加密模式的hash對象

new(name, string='')
     """
     Return a new hashing object using the named algorithm;
     optionally initialized with a string.
     """
h = hashlib.new('md5')
print h     #<md5 HASH object @ 000000000260BDB0>
h2 = hashlib.new('ripemd160','what')
print h2    #<ripemd160 HASH object @ 000000000271B9F0>
h.update('beginman')
print h.hexdigest() #666fc5baa93a7fb207c5bfff03b67732
#等效
s = hashlib.md5()
s.update('beginman')
print s.hexdigest() #666fc5baa93a7fb207c5bfff03b67732

print h2.hexdigest()    #9c1185a5c5e9fc54612808977ee8f548b2258d31

常用屬性:

#屬性
print hashlib.algorithms    #('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')    列出所有加密算法
print h.digest_size         #16 產生的散列的字節大小。 
print h.block_size          #64 The internal block size of the hash algorithm in bytes.

常用方法:

hash.update(arg) #更新哈希對象以字符串參數,如果同一個hash對象重復調用該方法,則m.update(a); m.update(b) is equivalent to m.update(a+b).

hash.digest()#返回摘要,作為二進制數據字符串值,

hash.hexdigest()#返回摘要,作為十六進制數據字符串值,

hash.copy() #復制

6、xml 

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

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

 1、解析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對象
利用ElementTree.parse解析文件

 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

節點功能一覽表
View Code

由於 每個節點 都具有以上的方法,並且在上一步驟中解析時均得到了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)
View Code

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)
View Code

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')

解析字符串方式,修改,保存
View Code
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')

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

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 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()
View Code

7、requests

Python標准庫中提供了:urllib等模塊以供Http請求,但是,它的 API 太渣了。它是為另一個時代、另一個互聯網所創建的。它需要巨量的工作,甚至包括各種方法覆蓋,來完成最簡單的任務。

import urllib.request


f = urllib.request.urlopen('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508')
result = f.read().decode('utf-8')
View Code
import urllib.request

req = urllib.request.Request('http://www.example.com/')
req.add_header('Referer', 'http://www.python.org/')
r = urllib.request.urlopen(req)

result = f.read().decode('utf-8')

發送攜帶請求頭的GET請求
帶請求頭的GET請求

注:更多見Python官方文檔:https://docs.python.org/3.5/library/urllib.request.html#module-urllib.request

Requests 是使用 Apache2 Licensed 許可證的 基於Python開發的HTTP 庫,其在Python內置模塊的基礎上進行了高度的封裝,從而使得Pythoner進行網絡請求時,變得美好了許多,使用Requests可以輕而易舉的完成瀏覽器可有的任何操作。

1、安裝模塊

pip3 install requests

2、使用模塊

# 1、無參數實例
 
import requests
 
ret = requests.get('https://github.com/timeline.json')
 
print(ret.url)
print(ret.text)
 
 
 
# 2、有參數實例
 
import requests
 
payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.get("http://httpbin.org/get", params=payload)
 
print(ret.url)
print(ret.text)

GET請求
GET請求
# 1、基本POST實例
 
import requests
 
payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.post("http://httpbin.org/post", data=payload)
 
print(ret.text)
 
 
# 2、發送請求頭和數據實例
 
import requests
import json
 
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
 
ret = requests.post(url, data=json.dumps(payload), headers=headers)
 
print(ret.text)
print(ret.cookies)
POST請求
requests.get(url, params=None, **kwargs)
requests.post(url, data=None, json=None, **kwargs)
requests.put(url, data=None, **kwargs)
requests.head(url, **kwargs)
requests.delete(url, **kwargs)
requests.patch(url, data=None, **kwargs)
requests.options(url, **kwargs)
 
# 以上方法均是在此方法的基礎上構建
requests.request(method, url, **kwargs)

其他請求
其他請求

更多requests模塊相關的文檔見:http://cn.python-requests.org/zh_CN/latest/

3、Http請求和XML實例

實例:檢測QQ賬號是否在線

import urllib
import requests
from xml.etree import ElementTree as ET

# 使用內置模塊urllib發送HTTP請求,或者XML格式內容
"""
f = urllib.request.urlopen('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508')
result = f.read().decode('utf-8')
"""


# 使用第三方模塊requests發送HTTP請求,或者XML格式內容
r = requests.get('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508')
result = r.text

# 解析XML格式內容
node = ET.XML(result)

# 獲取內容
if node.text == "Y":
    print("在線")
else:
    print("離線")
檢測QQ是否在線

實例:查看火車停靠信息

import urllib
import requests
from xml.etree import ElementTree as ET

# 使用內置模塊urllib發送HTTP請求,或者XML格式內容
"""
f = urllib.request.urlopen('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=')
result = f.read().decode('utf-8')
"""

# 使用第三方模塊requests發送HTTP請求,或者XML格式內容
r = requests.get('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=')
result = r.text

# 解析XML格式內容
root = ET.XML(result)
for node in root.iter('TrainDetailInfo'):
    print(node.find('TrainStation').text,node.find('StartTime').text,node.tag,node.attrib)
火車停靠信息

8、logging 

logging 專門用來打印日志, 其實就是各種寫數據流的封裝. 由於擴展性好且線程安全的, 設置靈活所以使用比較廣泛.從一個使用場景開始

開發一個日志系統, 既要把日志輸出到控制台, 還要寫入日志文件

單文件操作:

import logging
  
  
logging.basicConfig(filename='log.log',
                    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',
                    level=10)
  
logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical')
logging.log(10,'log')

設置logger的level, level有以下幾個級別:

CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0

如果把logger的level設置為INFO,那么小於INFO級別的日志都不輸出, 大於等於INFO級別的日志都輸出

 Formatter 格式化輸出:

%(name)s Logger的名字 
%(levelno)s 數字形式的日志級別 
%(levelname)s 文本形式的日志級別 
%(pathname)s 調用日志輸出函數的模塊的完整路徑名,可能沒有 
%(filename)s 調用日志輸出函數的模塊的文件名 
%(module)s 調用日志輸出函數的模塊名 
%(funcName)s 調用日志輸出函數的函數名 
%(lineno)d 調用日志輸出函數的語句所在的代碼行 
%(created)f 當前時間,用UNIX標准的表示時間的浮 點數表示 
%(relativeCreated)d 輸出日志信息時的,自Logger創建以 來的毫秒數 
%(asctime)s 字符串形式的當前時間。默認格式是 “2003-07-08 16:49:45,896”。逗號后面的是毫秒 
%(thread)d 線程ID。可能沒有 
%(threadName)s 線程名。可能沒有 
%(process)d 進程ID。可能沒有 
%(message)s用戶輸出的消息

對於上述記錄日志的功能,只能將日志記錄在單文件中,如果想要設置多個日志文件,logging.basicConfig將無法完成,需要自定義文件和日志操作對象。

# 定義文件
file_1_1 = logging.FileHandler('l1_1.log', 'a')
fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s")
file_1_1.setFormatter(fmt)

file_1_2 = logging.FileHandler('l1_2.log', 'a')
fmt = logging.Formatter()
file_1_2.setFormatter(fmt)

# 定義日志
logger1 = logging.Logger('s1', level=logging.ERROR)
logger1.addHandler(file_1_1)
logger1.addHandler(file_1_2)


# 寫日志
logger1.critical('1111')

日志(一)
View Code
# 定義文件
file_2_1 = logging.FileHandler('l2_1.log', 'a')
fmt = logging.Formatter()
file_2_1.setFormatter(fmt)

# 定義日志
logger2 = logging.Logger('s2', level=logging.INFO)
logger2.addHandler(file_2_1)

日志(二)
View Code

如上述創建的兩個日志對象

  • 當使用【logger1】寫日志時,會將相應的內容寫入 l1_1.log 和 l1_2.log 文件中
  • 當使用【logger2】寫日志時,會將相應的內容寫入 l2_1.log 文件中

9、configparser

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

# 注釋1
;  注釋2

[section1] # 節點
k1 = v1    # 值  options
k2:v2       #

[section2] # 節點
k1 = v1    #

指定格式
格式

1、獲取所有節點:

import configparser

config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.sections()
print(ret)
sections獲取節點

2、獲取指定節點下所有的鍵值對

import configparser
 
config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.items('section1')
print(ret)
items獲取所有的鍵值對

3、獲取指定節點下所有的建

import configparser

config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.options('section1')
print(ret
options獲取所有的鍵

4、獲取指定節點下指定key的值

import configparser
 
config = 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)
View Code

5、檢查、刪除、添加節點

import configparser
r = configparser.ConfigParser()
r.read('conf',encoding='utf-8')

print(r.has_section('o1'))  #檢查節點是否存在
檢查
mport configparser
r = configparser.ConfigParser()
r.read('conf',encoding='utf-8')
r.remove_section('hello')
r.write(open('conf','w'))
刪除
import configparser
r = configparser.ConfigParser()
r.read('conf',encoding='utf-8')

r.add_section('hello3')
r.write(open('conf','w'))  #w只寫相應的節點不清空內容,a追加 會復制一份內容然后追加
添加

6、檢查、刪除、設置指定組內的鍵值對

import configparser
r = configparser.ConfigParser()
r.read('conf',encoding='utf-8')

r.has_option('hello','arge') #給定節點和節點下的key 返回是否存在
檢查
import configparser
r = configparser.ConfigParser()
r.read('conf',encoding='utf-8')

r.remove_option('hello','arge') #給定節點和節點下的key 刪除
r.write(open('conf','w'))
刪除
import configparser
r = configparser.ConfigParser()
r.read('conf',encoding='utf-8')
r.set('hello','arge','19')  #節點名,key,values 進行添加
r.write(open('conf','w'))
設置 

10、subprocess

subprocess包主要功能是執行外部的命令和程序。

call()

父進程等待子進程完成,執行命令,返回狀態碼

ret = subprocess.call(["ls", "-l"], shell=False)
ret = subprocess.call("ipconfig", shell=True)

check_call() 

父進程等待子進程完成,如果執行狀態碼是 0 ,則返回0,否則拋異常

subprocess.check_call(["ls", "-l"])
subprocess.check_call("exit 1", shell=True)

check_output()

父進程等待子進程完成,返回子進程向標准輸出的輸出結果,如果狀態碼是 0 ,則返回執行結果,否則拋異常

這三個函數的使用方法相類似,我們以subprocess.call()來說明:

import subprocess
rc = subprocess.call(["ls","-l"])

我們將程序名(ls)和所帶的參數(-l)一起放在一個表中傳遞給subprocess.call(),可以通過一個shell來解釋一整個字符串:

import subprocess
out = subprocess.call("ls -l", shell=True)
out = subprocess.call("cd ..", shell=True)

我們使用了shell=True這個參數。這個時候,我們使用一整個字符串,而不是一個表來運行子進程。Python將先運行一個shell,再用這個shell來解釋這整個字符串。

subprocess.Popen(...)

 參數:

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()函數,用於設置子進程的一些屬性,如:主窗口的外觀,進程的優先級等等 
View Code

 實例:

import subprocess
ret1 = subprocess.Popen(["mkdir","t1"])
ret2 = subprocess.Popen("mkdir t2", shell=True)
View Code

終端輸入的命令分為兩種:

  • 輸入即可得到輸出,如:ifconfig
  • 輸入進行某環境,依賴再輸入,如:python
import subprocess
obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',)# 先進入到目錄 然后創建t3
View Code
import  subprocess

a = subprocess.Popen(["python"],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)
a.stdin.write("print('hello')\n")
a.stdin.write("print('hello1')")
a.stdin.close()

cmd_out = a.stdout.read()
a.stdout.close()
cmd_error=a.stderr.read()
a.stderr.close()
print(cmd_out)
print(cmd_error)
標准輸入、輸出
import subprocess

obj = 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)
View Code
import subprocess

obj = 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)
View Code

11、shutil

shutil.copyfileobj(fsrc, fdst[, length])

 

將文件內容拷貝到另一個文件中

import shutil

shutil.copyfileobj(open('conf','r'),open('log.log','w'))
View Code

shutil.copyfile(src, dst)

拷貝文件

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

shutil.copymode(src, dst)
僅拷貝權限。內容、組、用戶均不變

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

shutil.copystat(src, dst)
拷貝狀態的信息,包括:mode bits, atime, mtime, flags

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

shutil.copy(src, dst)
拷貝文件和權限

import shutil
 
shutil.copy('f1.log', 'f2.log')
View Code

shutil.copy2(src, dst)
拷貝文件和狀態信息

import shutil
 
shutil.copy2('f1.log', 'f2.log')
View Code

shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
遞歸的去拷貝文件夾

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

shutil.rmtree(path[, ignore_errors[, onerror]])
遞歸的去刪除文件

import shutil
 
shutil.rmtree('folder1')
View Code

shutil.move(src, dst)
遞歸的去移動文件,它類似mv命令,其實就是重命名。

import shutil
 
shutil.move('folder1', 'folder3')
View Code

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 shutil
ret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')
  
  
#將 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目錄
import shutil
ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')
View Code

shutil 對壓縮包的處理是調用 ZipFile 和 TarFile 兩個模塊來進行的,詳細:

import zipfile

# 壓縮
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()

# 解壓
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()
z.close()

zipfile解壓縮
zipfile
import tarfile

# 壓縮
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log')
tar.close()

# 解壓
tar = tarfile.open('your.tar','r')
tar.extractall()  # 可設置解壓地址
tar.close()

tarfile解壓縮
tarfile

字符串格式化

1、百分號方式

%[(name)][flags][width].[precision]typecode
  • (name)      可選,用於選擇指定的key
  • flags          可選,可供選擇的值有:
    • +       右對齊;正數前加正好,負數前加負號;
    • -        左對齊;正數前無符號,負數前加負號;
    • 空格    右對齊;正數前加空格,負數前加負號;
    • 0        右對齊;正數前無符號,負數前加負號;用0填充空白處
  • width         可選,占有寬度
  • .precision   可選,小數點后保留的位數
  • typecode    必選
    • s,獲取傳入對象的__str__方法的返回值,並將其格式化到指定位置
    • r,獲取傳入對象的__repr__方法的返回值,並將其格式化到指定位置
    • c,整數:將數字轉換成其unicode對應的值,10進制范圍為 0 <= i <= 1114111(py27則只支持0-255);字符:將字符添加到指定位置
    • o,將整數轉換成 八  進制表示,並將其格式化到指定位置
    • x,將整數轉換成十六進制表示,並將其格式化到指定位置
    • d,將整數、浮點數轉換成 十 進制表示,並將其格式化到指定位置
    • e,將整數、浮點數轉換成科學計數法,並將其格式化到指定位置(小寫e)
    • E,將整數、浮點數轉換成科學計數法,並將其格式化到指定位置(大寫E)
    • f, 將整數、浮點數轉換成浮點數表示,並將其格式化到指定位置(默認保留小數點后6位)
    • F,同上
    • g,自動調整將整數、浮點數轉換成 浮點型或科學計數法表示(超過6位數用科學計數法),並將其格式化到指定位置(如果是科學計數則是e;)
    • G,自動調整將整數、浮點數轉換成 浮點型或科學計數法表示(超過6位數用科學計數法),並將其格式化到指定位置(如果是科學計數則是E;)
    • %,當字符串中存在格式化標志時,需要用 %%表示一個百分號

注:Python中百分號格式化是不存在自動將整數轉換成二進制表示的方式

這里列出的格式化符合都比較簡單,唯一想要強調一下的就是"%s"和"%r"的差別。看個簡單的代碼:

string = "Hello\tWill\n"

print "%s" %string
print "%r" %string
####output####
Hello Will
'Hello\tWill\n'

Python2.6開始,新增了一種格式化字符串的函數str.format(),通過這個函數同樣可以對字符串進行格式化處理。在format()函數中,使用“{}”符號來當作格式化操作符。

2、Format方式

[[fill]align][sign][#][0][width][,][.precision][type]
    • ll           【可選】空白處填充的字符
    • align        【可選】對齊方式(需配合width使用)
      • <,內容左對齊
      • >,內容右對齊(默認)
      • =,內容右對齊,將符號放置在填充字符的左側,且只對數字類型有效。 即使:符號+填充物+數字
      • ^,內容居中
    • sign         【可選】有無符號數字
      • +,正號加正,負號加負;
      •  -,正號不變,負號加負;
      • 空格 ,正號空格,負號加負;
    • #            【可選】對於二進制、八進制、十六進制,如果加上#,會顯示 0b/0o/0x,否則不顯示
    • ,            【可選】為數字添加分隔符,如:1,000,000
    • width       【可選】格式化位所占寬度
    • .precision 【可選】小數位保留精度
    • type         【可選】格式化類型
      • 傳入” 字符串類型 “的參數
        • s,格式化字符串類型數據
        • 空白,未指定類型,則默認是None,同s
      • 傳入“ 整數類型 ”的參數
        • b,將10進制整數自動轉換成2進制表示然后格式化
        • c,將10進制整數自動轉換為其對應的unicode字符
        • d,十進制整數
        • o,將10進制整數自動轉換成8進制表示然后格式化;
        • x,將10進制整數自動轉換成16進制表示然后格式化(小寫x)
        • X,將10進制整數自動轉換成16進制表示然后格式化(大寫X)
      • 傳入“ 浮點型或小數類型 ”的參數
        • e, 轉換為科學計數法(小寫e)表示,然后格式化;
        • E, 轉換為科學計數法(大寫E)表示,然后格式化;
        • f , 轉換為浮點型(默認小數點后保留6位)表示,然后格式化;
        • F, 轉換為浮點型(默認小數點后保留6位)表示,然后格式化;
        • g, 自動在e和f中切換
        • G, 自動在E和F中切換
        • %,顯示百分比(默認顯示小數點后6位)

下面直接通過一些簡單的例子演示format()函數的基本使用:

# 位置參數
print("{0} is {1} years old".format("Wilber", 28))
print("{} is {} years old".format("Wilber", 28))
print("Hi, {0}! {0} is {1} years old".format("Wilber", 28))

# 關鍵字參數
print("{name} is {age} years old".format(name = "Wilber", age = 28))

# 下標參數
li = ["Wilber", 28]
print("{0[0]} is {0[1]} years old".format(li))

# 填充與對齊
# ^、<、>分別是居中、左對齊、右對齊,后面帶寬度
# :號后面帶填充的字符,只能是一個字符,不指定的話默認是用空格填充
print('{:>8}'.format('3.14'))
print('{:<8}'.format('3.14'))
print('{:^8}'.format('3.14'))
print('{:0>8}'.format('3.14'))
print('{:a>8}'.format('3.14'))

# 浮點數精度
print('{:.4f}'.format(3.1415926))
print('{:0>10.4f}'.format(3.1415926))

# 進制
# b、d、o、x分別是二進制、十進制、八進制、十六進制
print('{:b}'.format(11))
print('{:d}'.format(11))
print('{:o}'.format(11))
print('{:x}'.format(11))
print('{:#x}'.format(11))
print('{:#X}'.format(11))

# 千位分隔符
print('{:,}'.format(15700000000))

計算器源碼

import  re

def mutl_div(arg):
    '''

    :param arg: 去除括號后的表達式 eg:'-40/5'
    :return: 返回計算的結果
    '''
    while True:
        arg = re.sub('\-\-', '+', arg)    #表達式里的符號替換
        arg = re.sub('\-\+', '-', arg)
        arg = re.sub('\+\-', '-', arg)
        arg = re.sub('\+\+', '+', arg)
        str_split = re.split(r'(\d+\.?\d*[*/]-?\d+\.?\d*)', arg,1) #匹配公式里的乘除-->從左至右
        if len(str_split) ==3:
            a,b,c = str_split[0],str_split[1],str_split[2]
            if '*' in b:
                d,e = b.split('*')
                ret = float(d) * float(e)
                arg = a + str(ret) + c
            else:
                d, e = b.split('/')
                ret = float(d) / float(e)
                arg = a + str(ret) + c
        else:
            str_ret = re.findall(r'-?\d+\.?\d*',arg)  #加減運算 思想獲取表達式里所有的'-' 然后進行相加
            w = 0
            for x in str_ret:
                w+=float(x)
            return w
a = input('公式:')
while True:
    b =a.replace(' ','')
    ret = re.split('\(([^()]+)\)', b, 1)
    if len(ret) == 3:
        a,b,c = re.split('\(([^()]+)\)', b, 1)
        rec = mutl_div(b)
        a = a + str(rec) + c
    else:
        red = mutl_div(a)
        print('計算結果:%s'%(red))
        print('eval結果:%s'%(float(eval(b))))
        break

  

 

 

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM