网站首页 > 博客文章 正文
正所谓磨刀不误砍柴工。结合昨天说的adb shell top指令,今天说的xlwt模块的调用以及Excel表格中文本格式的转换;我们就可以开始使用Python来编写CPU占用率的脚本。
一、脚本编写的框架
在脚本编写前我们需要打个框架,然后才能逐步往下走。
1、先打印出应用CPU占用率信息
2、筛选CPU占用率的数值以及包名
3、把筛选出的数据转换成Excel表格
二、脚本实例
import os
import xlwt
for i in range(5):
val = os.popen(r"adb shell top -d 3 -n 1 | findstr -e +包名 ").read()
f = open(r"C:\Users\Administrator\Desktop\123\21.txt", "a")
result = str(val)
f.write(result)
f.close()
print("ok")
f1 = open(r"C:\Users\Administrator\Desktop\123\21.txt")
for x in f1:
line1 = x.split(' ')[7:8]
line11 = str(line1)[2:4]
line2 = x.split(' ')[-1:]
line22 = str(line2)[2:-4]
result = (line11 +":"+line22) +"\n"
f2 = open(r"C:\Users\Administrator\Desktop\123\22.txt", "a")
f2.write(result)
f2.close()
else:
print("Finally finished")
f3 = open(r"C:\Users\Administrator\Desktop\123\22.txt")
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('CPU数据', cell_overwrite_ok=True)
sheet.write(0, 1, "包名")
sheet.write(0, 0, "CPU占用率")
x = 1
y = 0
while True:
line3 = f3.readline()
if not line3:
break
for i in line3.split(':'):
item = i.strip()
print(item)
sheet.write(x, y, item)
y += 1
x += 1
y = 0
workbook.save(r'C:\Users\Administrator\Desktop\123\22.xls')
以上就是脚本实例,大家有空可以多多练习。
猜你喜欢
- 2024-10-24 python处理excel文件03:xlwt模块写入excel文件
- 2024-10-24 经验 | Python 读写 Excel 文件第三方库汇总
- 2024-10-24 python实现爬取豆瓣电影Top250(python爬取豆瓣电影的流程)
- 2024-10-24 Python如何操作Excel,xlrd和xlwt类库的使用
- 2024-10-24 如何用Python读写Excel文件?最便捷的3种方式
- 2024-10-24 安全服务日常工作之大量端口状态扫描
- 2024-10-24 Python操作Excel库xlrd与xlwt常用操作详解
- 2024-10-24 Python读写Excel表格,就是这么简单粗暴又好用
- 2024-10-24 Python学习之爬取网页信息(python爬取网页数据步骤)
- 2024-10-24 pandas + xlwt制作格式化报表遇到的两个问题
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)