python 處理xls

之前我有寫過一篇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,要麼,往後各幾列繼續寫。不然會報錯。


其中遇到的這兩個問題最當人頭疼。不過,我的代碼也是很亂,不過列出來吧。供參考。

  1. import xlrd  
  2. import xlwt  
  3. import time  
  4. #求均值   
  5. def jun():  
  6.     fname = '1214.xls'  
  7.     bk = xlrd.open_workbook(fname)  
  8.     shxrange = range(bk.nsheets)  
  9.     try:  
  10.         sh = bk.sheet_by_name("wind")  
  11.     except:  
  12.         print "no sheet in %s named Sheet1" % fname  
  13.         return None  
  14.     nrows = sh.nrows  
  15.     ncols = sh.ncols  
  16.     print "nrows %d, ncols %d" % (nrows,ncols)     
  17.     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()
  1. #添加一個sheet   
  2.  ws = wb.add_sheet('speed')  
  3.  ws.write(0,0,'time')  
  4.  ws.write(0,1,'speed')  
  5.  ws.write(0,2,'speed-wind')  
  6.  all = 0  
  7.  wind = 0  
  8.  k = 1  
  9.  count = 0  
  10.  com = ()  
  11.  flag = 0  
  12.  stat = 0  
  13.  for i in range(1,nrows):  
  14.      count = count + 1  
  15.      time = sh.cell_value(i,1)  
  16.      a = xlrd.xldate_as_tuple(time, 0)  
  17.      speed = sh.cell_value(i,2)  
  18.      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)
  1.  #處理第一個數據   
  2.         if flag == 0:  
  3.             com = a  
  4.             all = all + speed  
  5.             wind = wind + w  
  6.             flag = 1  
  7.             continue  
  8.         if flag == 1:  
  9.             if a[3] == com[3and a[4] == com[4]:  
  10.                 all = all + speed  
  11.                 wind = wind + w  
  12.             else:#處理同一分鐘的數據                 
  13.                 time_out = str(com[0])+'-'+str(com[1])+'-'+str(com[2])+' '+str(com[3])+':'+str(com[4])+':00'  
  14.                 print time_out  
  15.                 j = all/(count-1)  
  16.                 m = wind/(count-1)  
  17.                 print j  
  18.                 ws.write(k,0,time_out)  
  19.                 ws.write(k,1,j)  
  20.                 ws.write(k,2,m)  
  21.                 k = k + 1  
  22.                   
  23.                 all = speed  
  24.                 wind = w  
  25.                 com = a  
  26.                 count = 1  
  27.       #處理最後一分鐘數據             
  28.     time_out = str(com[0])+'-'+str(com[1])+'-'+str(com[2])+' '+str(com[3])+':'+str(com[4])+':00'  
  29.     j = all/(count)  
  30.     m = wind/(count)  
  31.     print j  
  32.     ws.write(k,0,time_out)  
  33.     ws.write(k,1,j)  
  34.     ws.write(k,2,m)  
  35.     wb.save('result.xls')              
  36.   
  37. if  __name__ == '__main__':  
  38.     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()


 

摘自:http://blog.csdn.net/cogbee/article/details/27103711

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章