新手学做网站代码,百度站长工具网站提交,电子书制作公司网站,网站建设课程设计文献综述文章更新中.............
python中对Excel表格的操作#xff0c;主要用到两个包#xff0c;分别是xlrd和xlwt
1.xlrd主要用于读Excel表
2.xlwt则主要用于写Excel
python—xlrd
这里我先准备了一些济南天气的一些数据通过python程序先对数据有一个基本的了解#xff1a;
path…文章更新中.............
python中对Excel表格的操作主要用到两个包分别是xlrd和xlwt
1.xlrd主要用于读Excel表
2.xlwt则主要用于写Excel
python—xlrd
这里我先准备了一些济南天气的一些数据通过python程序先对数据有一个基本的了解
path F:\\文件存放处\\weather\\济南.xlsimport xlrd
import xlwt
from datetime import date, datetime先定义一个函数对我们所拥有的数据做一个基本的了解
def read_excel():# 打开文件workbook xlrd.open_workbook(path)# 获取所有的sheetprint(workbook.sheet_names()) # [sheet1, sheet2]sheet1_name workbook.sheet_names()[0]#sheet1 workbook.sheet_by_index(0)print(sheet1)sheet1 workbook.sheet_by_name(济南2018年4月份天气详情)print(sheet1)# 显示sheet的名称、行数、列数print(sheet1.name, sheet1.nrows, sheet1.ncols)# 获取整行和整列的值rows sheet1.row_values(5) # 第六行的数据cols sheet1.col_values(2) # 第三列的数据print(rows)print(cols)# 获取单元格内容(四种获取方式)print(sheet1.cell(1, 0).value.encode(utf-8))print(sheet1.cell_value(1, 0).encode(utf-8))print(sheet1.row_values(1)[0].encode(utf-8))print(sheet1.col_values(0)[1].encode(utf-8))# 获取单元格内容的数据类型print(sheet1.cell(1, 0).ctype)if __name__ __main__:read_excel()
运行结果如下
[济南2018年4月份天气详情, 济南2018年3月份天气详情, 济南2018年2月份天气详情, 济南2018年1月份天气详情, 济南2017年12月份天气详情, 济南2017年11月份天气详情, 济南2017年10月份天气详情, 济南2017年9月份天气详情, 济南2017年8月份天气详情, 济南2017年7月份天气详情, 济南2017年6月份天气详情, 济南2017年5月份天气详情, 济南2017年4月份天气详情, 济南2017年3月份天气详情, 济南2017年2月份天气详情, 济南2017年1月份天气详情, 济南2016年12月份天气详情, 济南2016年11月份天气详情, 济南2016年10月份天气详情, 济南2016年9月份天气详情, 济南2016年8月份天气详情, 济南2016年7月份天气详情, 济南2016年6月份天气详情, 济南2016年5月份天气详情, 济南2016年4月份天气详情, 济南2016年3月份天气详情, 济南2016年2月份天气详情, 济南2016年1月份天气详情, 济南2015年12月份天气详情, 济南2015年11月份天气详情, 济南2015年10月份天气详情, 济南2015年9月份天气详情, 济南2015年8月份天气详情, 济南2015年7月份天气详情, 济南2015年6月份天气详情, 济南2015年5月份天气详情, 济南2015年4月份天气详情, 济南2015年3月份天气详情, 济南2015年2月份天气详情, 济南2015年1月份天气详情, 济南2014年12月份天气详情, 济南2014年11月份天气详情, 济南2014年10月份天气详情, 济南2014年9月份天气详情, 济南2014年8月份天气详情, 济南2014年7月份天气详情, 济南2014年6月份天气详情, 济南2014年5月份天气详情, 济南2014年4月份天气详情, 济南2014年3月份天气详情, 济南2014年2月份天气详情, 济南2014年1月份天气详情, 济南2013年12月份天气详情, 济南2013年11月份天气详情, 济南2013年10月份天气详情, 济南2013年9月份天气详情, 济南2013年8月份天气详情, 济南2013年7月份天气详情, 济南2013年6月份天气详情, 济南2013年5月份天气详情, 济南2013年4月份天气详情, 济南2013年3月份天气详情, 济南2013年2月份天气详情, 济南2013年1月份天气详情, 济南2012年12月份天气详情, 济南2012年11月份天气详情, 济南2012年10月份天气详情, 济南2012年9月份天气详情, 济南2012年8月份天气详情, 济南2012年7月份天气详情, 济南2012年6月份天气详情, 济南2012年5月份天气详情, 济南2012年4月份天气详情, 济南2012年3月份天气详情, 济南2012年2月份天气详情, 济南2012年1月份天气详情, 济南2011年12月份天气详情, 济南2011年11月份天气详情, 济南2011年10月份天气详情, 济南2011年9月份天气详情, 济南2011年8月份天气详情, 济南2011年7月份天气详情, 济南2011年6月份天气详情, 济南2011年5月份天气详情, 济南2011年4月份天气详情, 济南2011年3月份天气详情, 济南2011年2月份天气详情, 济南2011年1月份天气详情]
xlrd.sheet.Sheet object at 0x000001F2F2631C50
xlrd.sheet.Sheet object at 0x000001F2F2631C50
济南2018年4月份天气详情 31 6
[2018-04-05, 8, 1, 阴, 西北风, 微风]
[最低气温, 20, 15, 6, 4, 1, 1, 5, 13, 17, 14, 16, 11, 6, 7, 11, 15, 18, 20, 22, 23, 11, 10, 9, 8, 16, 15, 17, 22, 21, 19]
b2018-04-01
b2018-04-01
b2018-04-01
b2018-04-01
1Process finished with exit code 0
1python读取Excel中单元格的内容返回的有5种类型即上面示例中的ctype
ctype: 0 empty, 1 string, 2 number, 3 date, 4 boolean, 5 error
那么我们先看一下我们所拥有的数据类型
# 获取单元格内容的数据类型print(sheet1.cell(1, 0).value, sheet1.cell(1, 0).ctype)print(sheet1.cell(1, 1).value, sheet1.cell(1, 1).ctype)print(sheet1.cell(1, 2).value, sheet1.cell(1, 2).ctype)print(sheet1.cell(1, 3).value, sheet1.cell(1, 3).ctype)print(sheet1.cell(1, 4).value, sheet1.cell(1, 4).ctype)print(sheet1.cell(1, 5).value, sheet1.cell(1, 5).ctype)
运行结果为
2018-04-01 1
30 1
20 1
晴 1
东南风 1
2级 1这么说我们所拥有的数据全部为string类型。就连日期都没有出现date类型内心不禁感到莞尔一笑。
当单元格的ctype 3 时说明该单元格的数据为date类型这时需要用xlrd的xldate_as_tuple来处理为date格式但是需要先判断单元格的ctype3时才能用此函数进行操作。
date_value xlrd.xldate_as_tuple(sheet1.cell_value(1, 0), workbook.datemode)if (sheet1.cell(row, col).ctype 3):date_value xlrd.xldate_as_tuple(sheet1.cell_value(row, col), workbook.datemode)
也可以将date类型在转化为str类型
date_value (2018, 4, 1, 0, 0)
date_change date(*date_value[:3]).strftime(%Y/%m/%d)
print(date_change)
print(type(date_change))
程序运行结果如下
2018/04/01
class str由上可知ctype的值不同则类型也不同处理情况与date类型类似这里就不一一解释了。2获取合并的单元格