由于本人对python不熟悉的原因,写这个程序浪费了很多少时间。也学到了不少东西,不多说了,上代码
-
#!/usr/bin/python
-
# -*- coding:utf8 -*-
-
-
import sys
-
import os
-
import xlwt
-
import time
-
import datetime
-
-
#传入参数
-
#文件名:a.20130520-20130526.csv
-
#所以用到了传参和日期函数
-
-
#生成指定日期
-
time_argv = sys.argv[1]
-
d = datetime.datetime.strptime(time_argv,"%Y%m%d")
-
last_time = d + date time.timedelta(day=-6)
-
last_time_now = last_time.strftime('%Y%m%d')
-
-
#对文件名称再命名
-
FILE_NAME1='a' + last_time_now + '_' +time_argv + '.csv'
-
FILE_NAME1='b' + last_time_now + '_' +time_argv + '.csv'
-
FILE_NAME1='c' + last_time_now + '_' +time_argv + '.csv'
-
-
#对xls文件初始化
-
wbk = xlwt.Workbook()
-
-
#加载
-
sheet名称
-
sheet_a = wbk.add_sheet(u'测试1')
-
sheet_b = wbk.add_sheet(u'测试2')
-
sheet_c = wbk.add_sheet(u'测试3')
-
-
#打开三个文件
-
fg_a = open(FILE_NAME1,'r')
-
fg_b = open(FILE_NAME2,'r')
-
fg_c = open(FILE_NAME3,'r')
-
-
#把文件和sheet名相关联,做一个字典
-
dic = {fg_a:sheet_a,fg_b:sheet_b,fg_c:sheet_c}
-
filename_list = [fg_a,fg_b,fg_c]
-
for filename in filename_list:
-
sheet_file = dic[filename]
-
if (filename,sheet_file) in dic.items()
-
i = 0
-
for line in filename:
-
line = line.strip() #去除字符串的首尾字符
-
m = line.split(",") #以逗号分割做成列表
-
i = i+1
-
j = 0
-
for k in m:
-
sheet_file.write(i,j,k.decode('utf8')) #i代表行,j代表列,k是sheet名称
-
j = j+1
-
wbk.save('test.xls')
-
-
for filename in filename_list:
-
filename.close()
阅读(5898) | 评论(1) | 转发(1) |