码迷,mamicode.com
首页 > 编程语言 > 详细

python出报表

时间:2018-11-21 11:03:00      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:ima   oca   交易   字符串   bit   with   银行卡   额外   文件   

 python出报表使用到了数据库访问,文件读写,字符串切片处理。还可以扩展到电子邮件的发送,异常处理以及定时批任务。

总之在学习中发现还是有蛮多乐趣在其中。

 1 #coding=utf-8
 2 __date__=20181120
 3 
 4 import cx_Oracle
 5 import os
 6 import time
 7 
 8 isExt=os.path.exists(V:/report/%s%time.strftime(%Y%m%d,time.localtime()))
 9 if not isExt:
10     os.mkdir(V:/report/%s%time.strftime(%Y%m%d,time.localtime()))
11 file_name=V:/report/%s/%s.txt%(time.strftime(%Y%m%d,time.localtime()),time.strftime(%Y%m%d,time.localtime()))
12 
13 sql=‘‘‘select t.merchant,
14        t.type,
15        aes128_decrypt(t.accountno) accountno,
16        t.amount,
17        t.seq,
18        t.date,
19        t.merchant_order_id,
20        t.merchant_order_date,
21        t.charge,
22        t.success_time,
23        decode(t.status,‘00‘,‘成功‘,‘01‘,‘失败‘) status
24   from hpay.trans_info_log t
25  where t.type = ‘debit‘
26    and t.status = ‘00‘
27 ‘‘‘
28 
29 conn=cx_Oracle.connect(username/password@host:port/SERVICE_NAME)
30 c=conn.cursor()
31 x=c.execute(sql)
32 results=x.fetchall()#得到总结果
33 with open(file_name,w) as f:#w表示写
34     f.write(总条数=%s\n商户号|交易类型|银行卡号|交易金额|交易流水号|交易时间|商户订单号|商户订单时间|交易手续费|交易成功时间|交易状态\n%len(results))
35 for re in results:#遍历每一行
36     with open(file_name,a) as w:#a表示追加
37         acctno=str(re[2])
38         newacctno=(acctno[:4]+acctno[-4:].rjust(len(acctno)-4,*))#对卡号进行特殊处理,卡号前4后4中间*号显示
39         w.write(str(re[0])+|+str(re[1])+|+str(newacctno)+|+str(re[3])+|+str(re[4])+|+str(re[5])+|+str(re[6])+|+str(re[7])+|+str(re[8])+|+str(re[9])+|+str(re[10])+\n)
40 c.close()
41 conn.close()
42     

运行代码,得到结果展示:

技术分享图片

额外补充一下字符串对齐各种方法的区别:

S.ljust(width,[fillchar]) #输出width个字符,S左对齐,不足部分用fillchar填充,默认的为空格。 
S.rjust(width,[fillchar]) #右对齐 
S.center(width, [fillchar]) #中间对齐 
S.zfill(width) #把S变成width长,并在右对齐,不足部分用0补足 

 

python出报表

标签:ima   oca   交易   字符串   bit   with   银行卡   额外   文件   

原文地址:https://www.cnblogs.com/mwg666/p/9993519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!