python实现持久化存储,操作表格,时间戳

 1 import xlrd,xlwt,pickle,time,datetime
 2 book = xlrd.open_workbook("练习.xlsx")
 3 sheet1 = book.sheet_by_index(0)
 4 rownum=sheet1.nrows
 5 data=[]
 6 for i in range(4, rownum):
 7     if i >= 76:
 8         cls={}
 9         cls['name']=sheet1.cell_value(i,0).strip()
10         data.append(cls)
11     elif(i-1)%3==0:
12         cls = {}
13         name=sheet1.cell_value(i,0).strip()#班级
14         cls['teacher'] = sheet1.cell_value(i + 1, 2).strip()
15         cls['con']=[i for  i in sheet1.row_values(i)[2:] if i!='']
16         cls['room'] = sheet1.cell_value(i + 2, 2).strip()
17         cls['name']=name
18         data.append(cls)
19 print(data)
20 
21 with open('data0.txt','wb') as f:
22     pickle.dump(data,f)
23 
24 wb = xlwt.Workbook(encoding='utf-8')
25 ws = wb.add_sheet('A Test Sheet')
26 
27 for i in range(0,24):
28     ws.write(i,0,data[i]['name'])
29     ws.write(i,1,data[i]['teacher'])
30     ws.write(i,2,data[i]['room'])
31     ws.write(i,3,data[i]['con'])
32 
33 for j in range(24,38):
34     ws.write(j, 0, data[j]['name'])
35 
36 s = time.time()
37 Time = time.localtime(s)
38 # print(Time)
39 Tme = time.strftime("%Y%m%d%H%M%S", Time)
40 print(Tme)
41 now = datetime.datetime.now()
42 print(now)
43 # print(ss)
44 wb.save(str(Tme)+'.xls')

 

s = time.time()是获取时间元年,为了测试用,可省略
Time = time.localtime(s)是获取本地时间,为元组形式
Tme = time.strftime("%Y%m%d%H%M%S", Time)将元组转换为时间戳
wb.save(str(Tme)+'.xls')只能用字符串拼接,并且Tme中不能有:.
 
 
posted @ 2019-09-09 08:48  King~~~  阅读(512)  评论(0编辑  收藏  举报