之前我有寫過一篇C++處理xls,不過,由於學習了python,就像用python寫寫,正好一個同學給了我一個8萬多的數據讓處理一下,簡單說格式如下:
6 | 2014-3-22 0:00 | 4.4 | 117 |
7 | 2014-3-22 0:00 | 4.4 | 114 |
8 | 2014-3-22 0:00 | 3.9 | 116 |
9 | 2014-3-22 0:00 | 3.8 | 115 |
10 | 2014-3-22 0:00 | 3.7 | 110 |
11 | 2014-3-22 0:00 | 3.3 | 110 |
12 | 2014-3-22 0:00 | 3.3 | 109 |
13 | 2014-3-22 0:00 | 3.3 | 272 |
14 | 2014-3-22 0:00 | 3 | 286 |
15 | 2014-3-22 0:00 | 3 | 285 |
16 | 2014-3-22 0:00 | 3.2 | 289 |
17 | 2014-3-22 0:00 | 3 | 287 |
18 | 2014-3-22 0:00 | 3.3 | 287 |
19 | 2014-3-22 0:00 | 3.4 | 287 |
20 | 2014-3-22 0:00 | 3.8 | 287 |
21 | 2014-3-22 0:01 | 3.7 | 286 |
22 | 2014-3-22 0:01 | 3.6 | 289 |
23 | 2014-3-22 0:01 | 3.6 | 288 |
24 | 2014-3-22 0:01 | 3.5 | 288 |
25 | 2014-3-22 0:01 | 3.3 | 288 |
26 | 2014-3-22 0:01 | 3.7 | 292 |
27 | 2014-3-22 0:01 | 3.4 | 289 |
28 | 2014-3-22 0:01 | 3.3 | 289 |
29 | 2014-3-22 0:01 | 3.5 | 291 |
30 | 2014-3-22 0:01 | 3.3 | 288 |
這個思路不難,就是,對比時間,然後將時間是同一分鐘的加起來求一個均值就OK了。C++寫代碼的話我想也會很容易。這裏正好不是python嗎,於是用python來寫一個,同時呢也做一個備份。
1、python呢,首先需要下載兩個庫,xlwt和xlrd,一個是寫一個是讀xls
地址:
https://pypi.python.org/pypi/xlrd
https://pypi.python.org/pypi/xlwt
下載解壓,然後在這個目錄下面,使用python setup.py install就可以了。你可以在IDLE試着import xlrd試試,不報錯就說明安裝好了。
2、寫代碼,這個呢,網上有很多很多,一抓一大把的測試代碼,然後你只需要修改一下就可以成爲自己的了。我這裏也貼出我自己的代碼。然後說說我在做的過程中遇到的問題。
第一、時間問題,
從xls讀出來的時間,是一個float類型的,怎麼辦?我不能要這個時間吧,不然怎麼比較兩個數據是不是同一分鐘的?網上也是各種資料,但是解決辦法卻是非常的簡單。xlrd自帶的函數
a = xlrd.xldate_as_tuple(time, 0)
a就是一個元組了。
比如,xls裏面的時間是2014-05-22 19:02:11
那麼,你直接
time = sh.cell_value(i,1)
獲取這個時間,然後print是一個float類型的數字,但是經過a = xlrd.xldate_as_tuple(time, 0)函數轉換,a輸出就是一個元組,(2014,5,22,19,2,11,-,-)這個元組後面還有幾個,我們可以不管,我們只需要知道前面幾個代表的意思就夠了。明顯時間改怎麼比較顯而易見了吧。我們也不需要轉換什麼格式。
第二,xlwt這個最多支持的36656,向xls寫數據的時候,千萬注意了
ws.write(k,0,time_out)這個代碼,k不能超過36656,如果超過了,那麼重新建一個sheet,要麼,往後各幾列繼續寫。不然會報錯。
其中遇到的這兩個問題最當人頭疼。不過,我的代碼也是很亂,不過列出來吧。供參考。
- import xlrd
- import xlwt
- import time
- #求均值
- def jun():
- fname = '1214.xls'
- bk = xlrd.open_workbook(fname)
- shxrange = range(bk.nsheets)
- try:
- sh = bk.sheet_by_name("wind")
- except:
- print "no sheet in %s named Sheet1" % fname
- return None
- nrows = sh.nrows
- ncols = sh.ncols
- print "nrows %d, ncols %d" % (nrows,ncols)
- wb = xlwt.Workbook()
import xlrd
import xlwt
import time
#求均值
def jun():
fname = '1214.xls'
bk = xlrd.open_workbook(fname)
shxrange = range(bk.nsheets)
try:
sh = bk.sheet_by_name("wind")
except:
print "no sheet in %s named Sheet1" % fname
return None
nrows = sh.nrows
ncols = sh.ncols
print "nrows %d, ncols %d" % (nrows,ncols)
wb = xlwt.Workbook()
- #添加一個sheet
- ws = wb.add_sheet('speed')
- ws.write(0,0,'time')
- ws.write(0,1,'speed')
- ws.write(0,2,'speed-wind')
- all = 0
- wind = 0
- k = 1
- count = 0
- com = ()
- flag = 0
- stat = 0
- for i in range(1,nrows):
- count = count + 1
- time = sh.cell_value(i,1)
- a = xlrd.xldate_as_tuple(time, 0)
- speed = sh.cell_value(i,2)
- w = sh.cell_value(i,3)
#添加一個sheet
ws = wb.add_sheet('speed')
ws.write(0,0,'time')
ws.write(0,1,'speed')
ws.write(0,2,'speed-wind')
all = 0
wind = 0
k = 1
count = 0
com = ()
flag = 0
stat = 0
for i in range(1,nrows):
count = count + 1
time = sh.cell_value(i,1)
a = xlrd.xldate_as_tuple(time, 0)
speed = sh.cell_value(i,2)
w = sh.cell_value(i,3)
- #處理第一個數據
- if flag == 0:
- com = a
- all = all + speed
- wind = wind + w
- flag = 1
- continue
- if flag == 1:
- if a[3] == com[3] and a[4] == com[4]:
- all = all + speed
- wind = wind + w
- else:#處理同一分鐘的數據
- time_out = str(com[0])+'-'+str(com[1])+'-'+str(com[2])+' '+str(com[3])+':'+str(com[4])+':00'
- print time_out
- j = all/(count-1)
- m = wind/(count-1)
- print j
- ws.write(k,0,time_out)
- ws.write(k,1,j)
- ws.write(k,2,m)
- k = k + 1
- all = speed
- wind = w
- com = a
- count = 1
- #處理最後一分鐘數據
- time_out = str(com[0])+'-'+str(com[1])+'-'+str(com[2])+' '+str(com[3])+':'+str(com[4])+':00'
- j = all/(count)
- m = wind/(count)
- print j
- ws.write(k,0,time_out)
- ws.write(k,1,j)
- ws.write(k,2,m)
- wb.save('result.xls')
- if __name__ == '__main__':
- jun()
#處理第一個數據
if flag == 0:
com = a
all = all + speed
wind = wind + w
flag = 1
continue
if flag == 1:
if a[3] == com[3] and a[4] == com[4]:
all = all + speed
wind = wind + w
else:#處理同一分鐘的數據
time_out = str(com[0])+'-'+str(com[1])+'-'+str(com[2])+' '+str(com[3])+':'+str(com[4])+':00'
print time_out
j = all/(count-1)
m = wind/(count-1)
print j
ws.write(k,0,time_out)
ws.write(k,1,j)
ws.write(k,2,m)
k = k + 1
all = speed
wind = w
com = a
count = 1
#處理最後一分鐘數據
time_out = str(com[0])+'-'+str(com[1])+'-'+str(com[2])+' '+str(com[3])+':'+str(com[4])+':00'
j = all/(count)
m = wind/(count)
print j
ws.write(k,0,time_out)
ws.write(k,1,j)
ws.write(k,2,m)
wb.save('result.xls')
if __name__ == '__main__':
jun()