- 准备数据,这里用了一份top导出的txt文件
- 安装pycharm,python3(安装xlrd、xlwt、xlutils、openpyxl、sys)
- 在pycarm新建项目,新建一个py文件
- `
import time
import sys
sys.path.extend(['D:\\ProgramFiles\\python3\\Lib\\site-packages'])
import xlrd
import xlwt
import xlutils.copy
from openpyxl import Workbook,load_workbook
- 打开excel新建一个sheet页
book_name_xlsx = './top.xlsx'
wb = Workbook()
wb.save(book_name_xlsx)
wb=load_workbook(book_name_xlsx)
sheetming = time.strftime('%Y%m%d%H%M%S')
sheet=wb.create_sheet(sheetming)
- 将txt的数据导入到excel的sheet中,本次使用的数据大概如下
aa=[]
f = open('./top.txt')
for line in f.readlines():
data = line.split('\n\t')
for str1 in data:
sub_str = str1.split(' ')
aa.append(sub_str)
for i in range(len(aa)):
sheet.append(aa[i])
wb.save(book_name_xlsx)
print("写入数据成功!")
- 选择要使用的数据
wb = load_workbook ('./top.xlsx')
ws = wb[sheetming]
a = ws.max_row
v=[]
z=[]
b=[]
i=0
while i < a:
i = i+1
n = ws.cell (i,17).value
m = n.strip('M')
Y = float(m)
b.insert(i, Y)
p = ws.cell (i,21).value
o = float(p)
v.insert(i, o)
z = list(v)
- 使用计算公式
def choose(s):
sum = 0
all = 0
maxnum = max(s)
for i in s:
sum = sum + 1
all = all + i
average = all / sum
print(str("最大值{0},平均值{1}").format(maxnum, average))
- 调用函数获取计算结果
def main_res():
s = b
print("res",end='')
choose(s)
def main_cpu():
s = z
print("CPU",end='')
choose(s)
main_res()
main_cpu()
- 在pycharm的运行结果如下:
#记录学习使用,第二部分为在dos命令窗口运行的记录