Python中的數據類型轉換舉例及腳本統計服務器內存實例 原

統計系統剩餘的內存 

    In [1]: s1 = 'abc'

    In [2]: help(s1.startswith)

    Help on built-in function startswith:

    startswith(...)

    S.startswith(prefix[, start[, end]]) -> bool

    Return True if S starts with the specified prefix, False otherwise.

    With optional start, test S beginning at that position.

    With optional end, stop comparing S at that position.

    prefix can also be a tuple of strings to try.

    (END)

cat /proc/meminfo

    #!/usr/bin/python

    with open('/proc/meminfo') as fd:

        for line in fd:

            if line.startswith('MemTotal:'):

                total = line.split()[1]

                continue

            if line.startswith('MemFree:'):

                free = line.split()[1]

                break

    print "%.2f" % (int(free)/1024.0)+'M'

數據類型轉換計算(計算mac地址)

10進制轉換成16進制:

    In [9]: hex(10)

    Out[9]: '0xa'

16進制轉換成10進制:

    In [8]: int('0xa',16)

    Out[8]: 10

    In [7]: int('aa',16)

    Out[7]: 170

純數字的字符串轉換成10進制:

    In [10]: int('10')

    Out[10]: 10

10進制轉換成字符串:

    In [11]: str(10)

    Out[11]: '10'

舉例:

原有服務器mac:02:42:0e:31:98:0b,寫腳本生成下一個網卡的mac,自動加1。

    #!/usr/bin/python

    macaddr = '02:42:0e:31:98:0b'

    prefix_mac = macaddr[:-3]

    last_two = macaddr[-2:]

    plus_one = int(last_two,16)+1

    if plus_one in range(10):

        new_last_two = hex(plus_one)[2:]

        new_last_two = '0' + new_last_two

    else:

        new_last_two = hex(plus_one)[2:]

    if len(new_last_two) == 1:

        new_last_two = '0' +new_last_two

    new_mac = prefix_mac + ':' + new_last_two

    print new_mac.upper()

數據類型轉換(列表與字典相互轉換)

查看幫助join()

    Help on built-in function join:

    join(...)

    S.join(iterable) -> string

    Return a string which is the concatenation of the strings in the

    iterable.  The separator between elements is S.

    (END)

字符串轉列表:list(string)

    In [36]: a = list('aaa')

    In [37]: type(a)

    Out[37]: list

    In [38]: a

    Out[38]: ['a', 'a', 'a']

列表轉字符串:'''.join(list)

    In [38]: a

    Out[38]: ['a', 'a', 'a']

    In [39]:l = a

    In [18]: l

    Out[18]: ['a', 'a', 'a']

    In [19]: ''.join(l)

    Out[19]: 'aaa'

    In [20]: ','.join(l)

    Out[20]: 'a,a,a'

    In [21]: '.'.join(l)

    Out[21]: 'a.a.a'

    In [22]: a= 'a'

    In [23]: help(a.join)

字符串轉元組:tuple(string)

    In [24]: s

    Out[24]: ['a', 'a', 'a']

    In [26]: tuple(s)

    Out[26]: ('a', 'a', 'a')

元組轉字符串:''.join(tuple)

    In [54]: type(a)

    Out[54]: tuple

    In [55]: a = str(a)

    In [56]: a

    Out[56]: "('a', 'b', 'c', 111)"

    In [57]: type(a)

    Out[57]: str

字典轉列表:

    In [28]: dic = {'a':1,'b':2}

    In [29]: dic

    Out[29]: {'a': 1, 'b': 2}

    In [30]: dic.items()

    Out[30]: [('a', 1), ('b', 2)]

列表轉字典:

    In [31]: l1 = dic.items()

    In [32]: l1

    Out[32]: [('a', 1), ('b', 2)]

    In [33]: type(l1)

    Out[33]: list

    In [34]: dict(l1)

    Out[34]: {'a': 1, 'b': 2}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章