Python 对 Excel 文件的读写

Excel 文件的读写:

安装 matplotlib:

这里写图片描述

conda install pandas

Excel 读的库: rd 可以看成 read

conda install xlrd

Excel 写的库:wt 可以看成 write

conda install xlwt

Excel 文件内容:
这里写图片描述

代码:

from __future__ import print_function
import pandas as pd
from pandas import read_excel

pd.set_option("display.max_columns", 4)
pd.set_option("display.max_rows", 6)

df = read_excel("./工作簿1.xlsx", "工作表1")
print(df)

df = read_excel("./工作簿1.xlsx", "工作表1", index_col=0, skiprows=3)

index_col:告诉程序哪一行是序号行,可以隐藏
skiprows:表示跳过 Excel 开头的几行

如果一个 Excel 里面有多个 Sheet ,则可以使用下面的方法来打开:

from __future__ import print_function
import pandas as pd
from pandas import read_excel

# 使用上下文管理器打开 Excel 文件
with pd.ExcelFile("./工作簿1.xlsx") as xls:
    for x in range(1, 3):
        df = read_excel(xls, "工作表{}".format(x), index_col=0, skiprows=0)
        print(df)

写 Excel 文件

import pandas as pd

df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], index=[0, 1, 2], columns=list("ABCD"))
df.to_excel("./hello.xls")

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值