利用Python解析Excel文件
来源:互联网 发布:c语言如何输入n个整数 编辑:程序博客网 时间:2024/06/05 17:34
# coding=utf-8#!/usr/bin/env python"""Your task is as follows:- read the provided Excel file- find and return the min, max and average values for the COAST region- find and return the time value for the min and max entries- the time values should be returned as Python tuplesPlease see the test function for the expected return format"""import xlrdfrom zipfile import ZipFiledatafile = "2013_ERCOT_Hourly_Load_Data.xls"def open_zip(datafile): with ZipFile('{0}.zip'.format(datafile), 'r') as myzip: myzip.extractall()def parse_file(datafile): workbook = xlrd.open_workbook(datafile) sheet = workbook.sheet_by_index(0) data = [[sheet.cell_value(r, col) for col in range(sheet.ncols)] for r in range(sheet.nrows)] #获取第一列的所有值(coast) cv = sheet.col_values(1, start_rowx = 1, end_rowx = None) maxval = max(cv) minval = min(cv) maxpos = cv.index(maxval) + 1 minpos = cv.index(minval) + 1 maxtime = sheet.cell_value(maxpos, 0) #表格中第0列是时间 realmaxtime = xlrd.xldate_as_tuple(maxtime, 0) mintime = sheet.cell_value(minpos, 0) #表格中第0列是时间 realmintime = xlrd.xldate_as_tuple(mintime, 0) data = { 'maxtime': realmaxtime, 'maxvalue': maxval, 'mintime': realmintime, 'minvalue': minval, 'avgcoast': sum(cv) / float(len(cv)) } return data ### example on how you can get the data #sheet_data = [[sheet.cell_value(r, col) for col in range(sheet.ncols)] for r in range(sheet.nrows)] ### other useful methods: # print "\nROWS, COLUMNS, and CELLS:" # print "Number of rows in the sheet:", # print sheet.nrows # print "Type of data in cell (row 3, col 2):", # print sheet.cell_type(3, 2) # print "Value in cell (row 3, col 2):", # print sheet.cell_value(3, 2) # print "Get a slice of values in column 3, from rows 1-3:" # print sheet.col_values(3, start_rowx=1, end_rowx=4) # print "\nDATES:" # print "Type of data in cell (row 1, col 0):", # print sheet.cell_type(1, 0) # exceltime = sheet.cell_value(1, 0) # print "Time in Excel format:", # print exceltime # print "Convert time to a Python datetime tuple, from the Excel float:", # print xlrd.xldate_as_tuple(exceltime, 0) data = parse_file(datafile)import pprintpprint.pprint(data)assert data['maxtime'] == (2013, 8, 13, 17, 0, 0)assert round(data['maxvalue'], 10) == round(18779.02551, 10)
0 0
- 利用Python解析Excel文件
- python 简单解析excel文件
- 利用Python解析CSV文件
- Python利用xlwt写Excel文件
- Python 利用 XSD 文件,解析 XML 文件
- python 利用lxml 解析xml文件
- 利用Python将多个excel文件合并为一个文件
- 利用Python将excel表格转换为json文件
- python:解析XML文件后写入EXCEL(一)
- python:解析XML文件后写入EXCEL(二)
- Python解析excel文件并存入sqlite数据库
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用JXL解析Excel文件(二)
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用POI解析Excel文件(二)
- 利用 js-xlsx 实现 Excel 文件导入并解析Excel数据成json格式的数据
- Python Excel解析
- python解析excel
- python 解析excel
- Python解析excel
- [数据库学习SQLSERVER] 学习笔记
- 八、分组函数/聚合函数/双行处理函数
- 关于zxing扫码界面的优化
- 148. Sort List
- 521. Longest Uncommon Subsequence I
- 利用Python解析Excel文件
- 如何在个人博客中插入Jsbin或者jsfiddle代码
- 快速排序
- ubuntu下解决端口被占用问题
- Android 性能:避免在Android上使用ENUM
- git 指令
- 计数排序
- Mysql索引注意事项
- const的各种用法