Python學習-20171229

1、統計系統剩餘的內存python

#linux下能夠直接查看/proc/meminfo文件進行查看統計系統內存linux

#!/usr/bin/python內存

#打開meminfo文件字符串

with open('meminfo.txt ') as fd:
    for line in fd:
        if line.startswith('MemTotal'):
            total = line.split()[1]
            continue
        if line.startswith('MemFree'):
            free = line.split()[1]
            break
#使用%f格式化字符串。並只保留2爲小數。
print '%.2f' % (int(free) / 1024)

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

數據類型轉換:it

十六進制轉換爲十進制使用:int()io

十進制轉換爲十六進制:hex()ast

十進制轉換字符串:str()數據類型

字符串轉化爲十進制必須爲純數字的字符串。統計

 

#練習題計算mac地址

#mac地址
macaddr='00:0C:29:51:49:A8'
#取最後兩位
last_two=macaddr[-2:]
prefix_ma=macaddr[:-3]
#16進制轉換爲10進制並+1
plus_one=int(last_two,16)+1
#判斷若是是數字須要補齊0
if plus_one in xrange(10):
    new_last_two = hex(plus_one)[2:]
    new_last_two = '0'+hex(plus_one)[2:]
else:
    new_last_two = hex(plus_one)[2:]
    if len(new_last_two)==1:
        new_last_two = '0' + hex(plus_one)
new_mac=prefix_ma+':'+new_last_two
#轉換爲大寫
print (new_mac.upper())

3、類型轉換(列表與字典互相轉換)

字符列表:

list(string)

字符串:

".jonin(list)"

字符串轉爲元組

tuple(string)

元組轉字符串

".jion(tuple)"

相關文章
相關標籤/搜索