【優達學城測評】Intro to XLRD
阿新 • • 發佈:2020-10-22
import xlrd
datafile="2013_ERCOT_Hourly_Load_Data.xls"
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)]
print"\nList Comprehension"
print data[3][2]
print"\nCells in a nested loop:"
for row in range(sheet.nrows):
for col in range(sheet.ncols):
if row==50:
print sheet.cell_value(row,col),
###other useful methods:
print "\nROWS,COLUMNS, and CELLS:"
print sheet.nrows
print "Type of data in cell (row 3, cols 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 "\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,form the Excel float:",
print xlrd.xldate_as_tuple(exceltime,0)
data={
"maxtime":(0,0,0,0,0,0),
"maxvalue":0,
"mintime":(0,0,0,0,0,0,),
"minvalue":0,
"avgcoast":0}
return data
data=parse_file(datafile)
output:
轉載於:https://my.oschina.net/Bettyty/blog/755889