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