python操作xlsx格式文件并读取
作者:不想秃头的晨晨 时间:2021-07-01 20:03:26
之前给大家介绍过python高手之路python处理excel文件(方法汇总) Python操作Excel之xlsx文件 今天继续围绕python xlsx格式文件的操作方法给大家介绍,具体内容如下:
一.准备工作
二 .xlrd库读取
首先安装xlrd库,安装方法:pip install xlrd
import xlrd
#打开excel
wb = xlrd.open_workbook('test_user_data.xlsx')
#按工作簿定位工作表
sh = wb.sheet_by_name('TestUserLogin')
print(sh.nrows)#有效数据行数
print(sh.ncols)#有效数据列数
print(sh.cell(0,0).value)#输出第一行第一列的值
print(sh.row_values(0))#输出第一行的所有值
#将数据和标题组合成字典
print(dict(zip(sh.row_values(0),sh.row_values(1))))
#遍历excel,打印所有数据
for i in range(sh.nrows):
print(sh.row_values(i))
输出结果:
三.pandas库读取
1.安装pandas: pip install pandas
2.代码如下
import pandas as pd
df = pd.read_excel('test_user_data.xlsx')
data=df.values
print("获取到所有的值:\n{}".format(data))
结果如下:
3.操作行列
读取列数
import pandas as pd
#读取第一列、第二列、第四列
df = pd.read_excel('test_user_data.xlsx',sheet_name='TestUserLogin',usecols=[0,1,3])
data = df.values
print(data)
读取行数
import pandas as pd
#读取第一行
df = pd.read_excel('test_user_data.xlsx',sheet_name='TestUserLogin',nrows=1)
data = df.values
print(data)
来源:https://blog.csdn.net/chenchen5152/article/details/117389616
标签:python,xlsx,格式文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django drf请求模块源码解析
2023-06-07 10:03:07
![](https://img.aspxhome.com/file/2023/9/95819_0s.jpg)
在Python的Django框架中编写编译函数
2022-01-04 16:53:34
Python中print()函数的用法详情
2021-01-11 21:35:17
![](https://img.aspxhome.com/file/2023/4/101724_0s.png)
正则表达式简介
2007-08-12 18:01:00
python snownlp情感分析简易demo(分享)
2021-07-18 04:32:35
![](https://img.aspxhome.com/file/2023/0/72250_0s.jpg)
Python 发送SMTP邮件的简单教程
2021-04-27 00:26:51
![](https://img.aspxhome.com/file/2023/1/67761_0s.png)
Python Matplotlib中使用plt.savefig存储图片的方法举例
2021-11-19 14:08:55
![](https://img.aspxhome.com/file/2023/1/79431_0s.png)
详解python中的Turtle函数库
2021-10-17 19:50:45
Python中字典的相关操作介绍
2021-03-16 07:48:54
Python 匹配任意字符(包括换行符)的正则表达式写法
2023-01-23 23:11:09
ASP分页和日期格式化为RFC822格式的办法
2008-11-21 15:46:00
Mootools 1.2教程(20)——选项卡效果(Tabs)
2008-12-26 18:19:00
解决SQLServer最大流水号的两个好方法
2009-01-13 14:15:00
ASP 三层架构 Error处理类
2011-03-16 11:06:00
TensorFlow Session会话控制&Variable变量详解
2023-06-21 12:37:42
![](https://img.aspxhome.com/file/2023/0/78650_0s.jpg)
python多线程方法详解
2023-10-16 02:46:31
python实现人机对战的五子棋游戏
2021-07-05 04:36:03
![](https://img.aspxhome.com/file/2023/0/65910_0s.jpg)
Python实现定时任务
2022-06-12 04:53:31
linux下安装apache与php;Apache+PHP+MySQL配置攻略
2023-11-14 15:43:46
python学习之新式类和旧式类讲解
2021-02-27 11:10:38
![](https://img.aspxhome.com/file/2023/1/108141_0s.png)