python使用pandas读写excel

依赖库安装:

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']])

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容