python写入xls
安装xlwt
windows,cmd,pip install xlwt
读取linux下的cat /proc/cpuinfo>cpuinfo
代码:
import xlwt #导入xlwt模块 #创建工作簿对象并指定编码格式,非常重要
workbook = xlwt.Workbook(encoding= "utf-8")
#创建sheet对象,并命名为test,,参数cell_overwrite_ok用于确认同一个cell单元是否可以重设值
sheet = workbook.add_sheet("test",cell_overwrite_ok= True)
with open("cpuinfo") as f: lines = f.readlines() for index,line in enumerate(lines): #print(index,line) lineSplit = line.split(":") #print(lineSplit) if len(lineSplit) == 2: key,value = lineSplit key = key.strip() value = value.strip() print(key + " " + value) sheet.write(index,0,key) sheet.write(index,1,value) workbook.save("cpu.xls")
文件cpuinfo
processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 63 model name : Intel(R) Xeon(R) CPU E5-4610 v3 @ 1.70GHz stepping : 2 cpu MHz : 1696.023 cache size : 25600 KB physical id : 0 siblings : 8 core id : 0 cpu cores : 8 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx lm constant_tsc rep_good unfair_spinlock pni cx16 hypervisor lahf_lm bogomips : 3392.04 clflush size : 64 cache_alignment : 64 address sizes : 42 bits physical, 48 bits virtual power management: