纳速健身

标题: python/pandas 读取Excel不同sheet的数据(或名称) [打印本页]

作者: awagink    时间: 2020-10-30 22:56
标题: python/pandas 读取Excel不同sheet的数据(或名称)

场景:nickname.xlsx有两个sheet,
sheet名称分别是:基本信息,用户昵称
如图:

现在想读取两个sheet的数据:

1.读取两个sheet名称:
sheet=pd.read_excel(‘nickname.xlsx’,sheet_name=None)
print(list(sheet.keys()))
for j in sheet.keys():
print(j)
注:此处把我的xlsx的文件路径省略
执行结果如图:

2.读取两个sheet内容:
sheet=pd.read_excel(‘nickname.xlsx’,sheet_name=None)
for j1 in sheet.values():
j1=j1.to_dict(orient=‘records’)
print(j1)
执行结果如图:

3.读取两个sheet的名称和内容:
sheet=pd.read_excel(‘nickname.xlsx’,sheet_name=None)
for k,v in sheet.items():
v = v.to_dict(orient=‘records’)
print(k,v)
执行结果如图:

完整代码:

最后注意:
1.sheet_name=None这个一定要加上。
不加的话,python默认只读取Excel的第一个sheet。
2.to_dict(orient=‘records’)这个表示把pandas的

上面的数据类型变成(字典列表)下面这种:

https://blog.csdn.net/qq_41810188/article/details/108336378?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param







欢迎光临 纳速健身 (https://www.nasue.com/) Powered by Discuz! X3.4