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

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

 #coding=utf-8
__date__='' import cx_Oracle
import os
import time isExt=os.path.exists('V:/report/%s'%time.strftime('%Y%m%d',time.localtime()))
if not isExt:
os.mkdir('V:/report/%s'%time.strftime('%Y%m%d',time.localtime()))
file_name='V:/report/%s/%s.txt'%(time.strftime('%Y%m%d',time.localtime()),time.strftime('%Y%m%d',time.localtime())) sql='''select t.merchant,
t.type,
aes128_decrypt(t.accountno) accountno,
t.amount,
t.seq,
t.date,
t.merchant_order_id,
t.merchant_order_date,
t.charge,
t.success_time,
decode(t.status,'00','成功','01','失败') status
from hpay.trans_info_log t
where t.type = 'debit'
and t.status = '00'
''' conn=cx_Oracle.connect('username/password@host:port/SERVICE_NAME')
c=conn.cursor()
x=c.execute(sql)
results=x.fetchall()#得到总结果
with open(file_name,'w') as f:#w表示写
f.write('总条数=%s\n商户号|交易类型|银行卡号|交易金额|交易流水号|交易时间|商户订单号|商户订单时间|交易手续费|交易成功时间|交易状态\n'%len(results))
for re in results:#遍历每一行
with open(file_name,'a') as w:#a表示追加
acctno=str(re[2])
newacctno=(acctno[:4]+acctno[-4:].rjust(len(acctno)-4,'*'))#对卡号进行特殊处理,卡号前4后4中间*号显示
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')
c.close()
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. C# 文字滚动特效(上下滚动)

    本程序改编至网上下载的一个自定义控件,原控件是左右滚动效果,类似于跑马灯效果,由于项目需要,改编为上下滚动. 前期没有实现自动折行,今天刚加上自动折行. using System; using Sys ...

  2. Android几种视频播放方式,VideoView、SurfaceView+MediaPlayer、TextureView+MediaPlayer,以及主流视频播放器开源项目

    简单的说下一Android的几种视频播放功能: 1.VideoView:最简单的视频播放 <FrameLayout xmlns:android="http://schemas.andr ...

  3. 数据库索引的数据结构b+树

    b+树的查找过程:如上图所示,如果要查找数据项29,那么首先会把磁盘块1由磁盘加载到内存,此时发生一次IO,在内存中用二分查找确定29在17和35之间,锁定磁盘块1的P2指针,             ...

  4. 对象的API

    entries keys values is assign create toSting ProetydefineProperty(obj,key,propety) Object.entries(), ...

  5. Maintaining ICM Parameters for Using SSL for As JAVA

    1770585 - How to configure SSL on the AS Java You can use this procedure to configure the necessary ...

  6. MySQL一千行笔记

    /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */ mysq ...

  7. zabbix自动发现华为,H3C交换机

    一.添加自动发现规则 1.ip范围尽量别太大 zabbix是通过ARP来搜索符合条件的主机的 2.团体名和交换机要一样.这个OID值是提取系统信息的 在OID这块遇到个坑 我用Getif查询到的是1. ...

  8. 全面超越Appium,使用Airtest超快速开发App爬虫

    想开发网页爬虫,发现被反爬了?想对 App 抓包,发现数据被加密了?不要担心,使用 Airtest 开发 App 爬虫,只要人眼能看到,你就能抓到,最快只需要2分钟,兼容 Unity3D.Cocos2 ...

  9. lambda函数式编程

    一.接口注解(@FunctionalInterface) @FunctionalInterface interface Interface1 { public void print(); } publ ...

  10. idea搭建ssm框架

    1.file-->new-->project-->maven.... 2.建立后的目录: 3.pom.xml依赖建立: <?xml version="1.0" ...