python读取excel里面的数据(附int变float解决方法)
时间:2019-09-21 11:32:22
收藏:0
阅读:212
好久没写了,今天来说说python读取excel的常见方法。首先需要用到xlrd模块,pip install xlrd 安装模块。
首先打开excel文件:
xl = xlrd.open_workbook(r‘D:\file\data.xlsx‘) 传文件路径
通过索引获取要操作的工作表
table = xl.sheets()[0]
获取第一行的内容,索引从0开始
row = table.row_values(0)
获取第一列的整列的内容
col = table.col_values(0)
获取单元格值,第几行第几个,索引从0开始
data = table.cell(2,0).value
pycharm读取数据后发现整数变成了小数
如图,手机号变小数:
解决办法:在整数内容前加上一个英文的引号即可
读取excel内容方法截图:
# todo 对excel的操作 import xlrd # todo 打开excle xl = xlrd.open_workbook(r‘D:\file\data.xlsx‘) #print(xl.read()) # todo 通过索引获取工作表 table = xl.sheets()[0] print(table) # 获取一共多少行 rows = table.nrows print(rows) # todo 获取第一行的内容,索引从0开始 row = table.row_values(0) print(row) # todo 获取第一列的整列的内容 col = table.col_values(0) print(col) # todo 获取单元格值,第几行第几个,索引从0开始 data = table.cell(3,0).value print(data)
原文:https://www.cnblogs.com/xiamaojjie/p/11561189.html
评论(0)