linqy的个人博客分享 http://blog.sciencenet.cn/u/linqy

博文

python 读取Excel

已有 2344 次阅读 2019-5-24 15:32 |系统分类:科研笔记

导入引用

import xlrd
import xlwt
inExcel = r"C:\Users\Administrator\Desktop\13456.xls"

第一步:读取Excel表的sheet

# 打开Excel文件
dataExcel = xlrd.open_workbook(inExcel)
print (dataExcel.sheet_names())  #获取sheet名称
print dataExcel.sheet_names() #获取sheet名称
print dataExcel.nsheets # 获取sheet数目

第二步:读取具体的sheet

table01 = dataExcel.sheet_by_index(0) #获取sheet
print table01.ncols   #获取sheet列数
print table01.nrows  #获取sheet行数

第三步:读取具体的数值

for i in range(1, table01.nrows):
    for j in range(table01.ncols):
        cellType = table01.cell(i, j).ctype   # 获取Excel的值的类型
        cellValue = table01.cell_value(i, j)  # 获取Excel的值


具体的示例:

# coding:utf-8
import xlrd
inExcel = r"C:\Users\Administrator\Desktop\13456.xls"
# 打开Excel文件
dataExcel = xlrd.open_workbook(inExcel)
print dataExcel.sheet_names() #获取sheet名称
table01 = dataExcel.sheet_by_index(0)
row_content = []  # 初始化一个列表,用来存储Excel的值
for i in range(1, table01.nrows):
    for j in range(table01.ncols):
        cellType = table01.cell(i, j).ctype   # 获取Excel的值的类型
        cellValue = table01.cell_value(i, j)  # 获取Excel的值
        row_content.append(cellValue)
print row_content


参考:

https://www.cnblogs.com/feiyueNotes/p/7786579.html 

https://www.cnblogs.com/lwf1990/p/7755192.html 



https://blog.sciencenet.cn/blog-3134052-1180947.html

上一篇:arcpy 给图层增加属性字段,同时解决Error 000464
下一篇:arcpy 获取 gdb 下的所有的数据集以及所有的图层
收藏 IP: 112.53.64.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-27 01:34

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部