依赖库安装:
pip install pandas
pandas处理Excel需要xlrd、openpyxl依赖包
pip install xlrd # 读
pip install openpyxl # 写
读写excel文件:
# 取个文件名
filename =self.host_ip +"_" + time.strftime("%Y%m%d_%H%M%S") +'_perfInfo.xlsx'
# 用准备好的数据生成一个 DataFrame对象
df = pandas.DataFrame(self.cpu_info)
print(df.values)
# 生成一个excel写入器的对象
writer = pandas.ExcelWriter(filename)
# 将数据写入excel文件
# sheet_name='cpu_info' 指定写入的sheet页名称
# index=False 不要写入器自带索引编号写入到excel文件中(None也是可以的)
# header=False 不要写入器自带头字段写入到excel文件中 (None也是可以的)
df.to_excel(writer, sheet_name='cpu_info', index=False, header=False)
writer.save()
#time.sleep(2)
# 读入excel文件
df1 = pandas.read_excel(filename)
# 获取指定列的excel数据
print(df1[['cpu_us', 'cpu_sy', 'cpu_id', 'cpu_wa']])
网友评论