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

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

  1. #coding=utf-8
  2. __date__=''
  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,'*'))#对卡号进行特殊处理,卡号前44中间*号显示
  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()

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

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

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

python读取数据库出txt报表的更多相关文章

  1. python读取数据库并把数据写入本地文件

    一,介绍 上周用jmeter做性能测试时,接口B传入的参数需要依赖接口A生成的借贷申请ID,接口A运行完需要把生成的借贷申请ID导出来到一个文件,作为参数传给接口B,刚开始的时候,手动去数据库倒, 倒 ...

  2. python读取数据库mysql报错

    昨天在学习PYTHON读取数据库的知识时,一直在报错,找不到原因. 最后同事说是语法错误. import sysreload(sys)sys.setdefaultencoding('gb18030') ...

  3. python读取数据库数据,读取出的中文乱码问题

    conn = pymysql.connect( host='127.0.0.1', port=3302, user='username', passwd='password', db=database ...

  4. python读取、写入txt文本内容

    转载:https://blog.csdn.net/qq_37828488/article/details/100024924 python常用的读取文件函数有三种read().readline().r ...

  5. python读取数据库数据有乱码怎么解决?

    http://blog.sina.com.cn/s/blog_6826662b0100yeze.html 简单暴力,直接上代码 conn = MySQLdb.connect(host = " ...

  6. python 读取数据库中文内容显示一堆问号

    需要在连接数据库时 设置编码格式 def select_db(self,db_name): self.conn = MySQLdb.connect( host = self.ip, port = se ...

  7. python 读取数据库时,datetime类型无法被json序列化--解决方案

    新增针对datetime的jsonencode: # -*- coding: utf-8 -*- import json from datetime import date, datetime cla ...

  8. 孤荷凌寒自学python第五十二天初次尝试使用python读取Firebase数据库中记录

    孤荷凌寒自学python第五十二天初次尝试使用python读取Firebase数据库中记录 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数 ...

  9. Python读取txt文件

    Python读取txt文件,有两种方式: (1)逐行读取 data=open("data.txt") line=data.readline() while line: print ...

随机推荐

  1. VMware 12 安装 Windows server 2008 系统

    一.准备工作 安装了VMware 12 的PC 准备windows  server 2008 的ISO操作系统文件 官网地址:https://www.microsoft.com/en-us/downl ...

  2. 自制按钮图标的两种方法: image sprite和svg字体文件

    用image sprite和svg字体文件这两种方法,都能够极大地减少小图形文件的数量, 从而减少服务器请求和带宽需求.提高网页的响应速度. 一.建立SVG字体文件 iconmoon 是一个在线工具, ...

  3. nodejs版 阿里云开放api签名算法

    阿里云 API 签名 github:https://github.com/liuyinglong/aliyun; npm :https://www.npmjs.com/package/aliyun-a ...

  4. MongoDB(3)--Java 操作 MongoDB 的 API

    引入mongoDb 依赖 <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-j ...

  5. sed的替换元字符的语法

    \(和\)用于保存正则表达式的一部分,而\1和\2用于保存回调的部分. 例如: 将sample.txt,内容如下 1...........55...........1010..........1010 ...

  6. DEPLOY YOUR WEBSITE TO GITHUB PAGES

    DEPLOY YOUR WEBSITE TO GITHUB PAGES Review Fantastic! You now have your site published on the public ...

  7. ReactiveX 学习笔记(25)使用 RxJS + Vue.js 调用 REST API

    JSON : Placeholder JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站. ...

  8. ExtJS4.2下将表单元素放在菜单时不能进行拷贝的问题解决办法

    通过浏览器F12我们发现,在菜单对应的dom元素上面,有几个系统附加的事件处理函数,只要我们将它去掉就可以了.示意代码如下: { xtype: "button", scope: z ...

  9. Linux上Oracle18C 系统表空间大小调整 adjust the system tablespace for oracle18c on linux

    1. 测试过程中,发现  system tablespace is not enough, need to adjustment 初步解决方案: 查询一下 system tablespace的大小 S ...

  10. 解决IIS配置问题

    解决网站运行一段时间会变慢的问题 http://blog.csdn.net/rryqsh/article/details/8156558 1. IIS 7 应用程序池自动回收关闭的解决方案 如果你正在 ...