python + Excel数据读取(更新)
2024-09-04 21:53:05
data.xlsx 数据如下:
import xlrd
#1.读取Excel数据
# table = xlrd.open_workbook("data.xlsx","r")
# print("获取excel的所有标签:",table.sheets())
# for sheet in table.sheets():
# print(sheet)
#2.读取第一个标签 第二个标签 ....
# table = xlrd.open_workbook("data.xlsx","r")
# sheetname = table.sheet_by_name("Sheet1")
# print("sheetname:",sheetname)
# sheetname1 = table.sheet_by_index(0)
# print("sheetname1:",sheetname1)
#3.读取excel数据指定的行数和列数 nrows 行数 ncols 列数
# table = xlrd.open_workbook("data.xlsx","r")
# sheet1 = table.sheet_by_index(0)
# print("Sheet1下面有{}行数据".format(sheet1.nrows))
# print("Sheet1下面有{}列数据".format(sheet1.ncols))
#4.获取指定的行数和列数 row_values 行数 col_values 列数
# table = xlrd.open_workbook("data.xlsx","r")
# sheet1 = table.sheet_by_index(0)
# onesheetrow = sheet1.row_values(0)
# print("第一行的数据:",onesheetrow) #第一行的数据: ['用户名', '密码', '预期结果']
# onesheetcol = sheet1.col_values(0)
# print("第一列的数据:",onesheetcol)
#第一列的数据: ['用户名', 18797813131.0, 18797813121.0, 18797813122.0, 18797813123.0, 18797813124.0]
#5.获取指定行和指定列的数据 cell
# table = xlrd.open_workbook("data.xlsx","r")
# sheet1 = table.sheet_by_index(0)
# row_col = sheet1.cell(0,0)
# print("第一行和第一列的数据:",row_col)
# row_col1 = sheet1.cell(2,1)
# print("第二行和第一列的数据:",row_col1)
#6.获取excel的用户名和密码
# table = xlrd.open_workbook("data.xlsx","r")
# sheet1 = table.sheet_by_index(0)
# rows = sheet1.nrows
# for i in range(1,rows): # 1,2,3,4,5
# print("当前的用户名为:",sheet1.row_values(i)[0],
# "密码为:",sheet1.row_values(i)[1],
# "获取的登录信息为:",sheet1.row_values(i)[2])
#7.写一个方法,输入行数,返回该行的所有数据
# def read_user(nrow=0):
# """读取用户名"""
# table = xlrd.open_workbook("data.xlsx","r")
# sheet1 = table.sheet_by_index(0)
# return sheet1.row_values(nrow)[0]
#
# def read_passwd(nrow):
# """读取密码"""
# table = xlrd.open_workbook("data.xlsx", "r")
# sheet1 = table.sheet_by_index(0)
# return sheet1.row_values(nrow)[1]
#
# # print(read_user(1),read_passwd(1))
#
# row_number = int(input("请输入行数"))
# print("第{}行的数据是:{}".format(row_number,read_user(row_number)))
#8.读取的数据存储在List的中
def readExcels():
tables = xlrd.open_workbook("data.xlsx", "r")
sheet = tables.sheet_by_index(0)
nrow = [] #定义空列表
for row in range(1,sheet.nrows):
nrow.append(sheet.row_values(row,start_colx=0,end_colx=sheet.ncols))
return nrow
print(readExcels())
最新文章
- java导入excel时遇到的版本问题
- ios - 文件保存路径的获取
- C语言中system()函数的用法总结(转)
- Cocos2d 学习资料推荐
- JAVA发送邮件工具类
- matlab和FPGA中无符号数和有符号数的转化(转)
- 1002: Prime Path
- Collections.shuffle源码阅读
- css cursor属性-显示的光标的类型(形状)的用法和定义
- POJ1015 &;&; UVA - 323 ~Jury Compromise(dp路径)
- Evaluation of Forwarding Efficiency in NFV-Nodes Toward Predictable Service Chain Performance
- JDBC操作MySQL数据
- 王者荣耀交流协会互评Beta版本--爱阅app
- centos部署redis主从
- magento中Model创建以及该Model对于数据库的增删改查
- Xlight FTP搭建FTP服务器教程
- java旅程(二) 基本语法
- package.json中devDependencies与dependencies的区别
- Linux五种I/O模型性能分析
- svn 操作字母的提示
热门文章
- JUC 并发编程--09, 阻塞队列: DelayQueue, PriorityBlockingQueue ,SynchronousQueue, 定时任务线程池: ScheduledThreadPoolExecutor
- 【NX二次开发】获取片体的边UF_MODL_ask_body_boundaries()
- Django基础之视图层
- LuatOS | 全新在线模拟器,随时随地发挥创意
- DBA入门相关知识介绍
- EEPROM工作原理透彻详解
- .NET解密得到UnionID
- Linux基本命令精讲
- Excel选择区域一次性替换小于200的数值
- 一篇技术博文引发的stylelint项目实践