Python处理Excel数据分组

描述:将Excel表中的数据按照每50个进行分组,并输出保存到不同的文件。

 1 #coding=utf-8
 2 import xlrd
 3 
 4 def read(file):
 5     data = xlrd.open_workbook(file)
 6     table = data.sheets()[0]
 7     nrows = table.nrows
 8     ncols = table.ncols
 9     
10 
11     n = 0
12     
13     filename = 1
14     fopen = open('d:/zemail/'+str(filename)+'.txt', 'w')
15 
16     for i in xrange(0, nrows):
17         rowValues = table.row_values(i)
18         n = n+1
19         if n%51 != 0:
20             for item in rowValues:
21                 #print item
22                 fopen.write(item+'\n')
23         else:
24             fopen.close()
25             filename = n
26             fopen = open('d:/zemail/'+str(filename)+'.txt', 'w')
27     fopen.close()
28         
29 
30 #def create(filename):
31     #if os.path.exists(fname):
32         #print "File already exists!"
33     #fopen = open(filename, 'w')
34     #fopen.close()
35 
36 if __name__ == '__main__':
37     path = "d:/email.xlsx"
38     read(path)

 

posted @ 2018-03-22 14:27  四季万花筒  阅读(1133)  评论(0编辑  收藏  举报