python脚本导数据-2-csv

2022/8/9 1:22:45

本文主要是介绍python脚本导数据-2-csv,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

需求:

工作日常中会有很多这样的情况,产品人员会提OA要求dba导数据(导出的数据放表格文件中),有时数据量要达到几千万,假如是人工导,要导好多次,时间消耗比较多。所以决定写脚本来导。

 

#!/usr/bin/python
import MySQLdb
import sys,os
import time
import datetime
import codecs
import csv

ip = 'xxx.xxx.xxx.xxx'
user = 'xxxx'
password = 'xxxxx'


class SQLgo():
def __init__(self, ip=None, user=None, password=None , db=None):
#def __init__(self, ip=None, user=None, password=None , db=None, socket=None):
self.ip = ip
self.user = user
self.db = db
# self.socket = socket
self.password=password
self.conn = MySQLdb.connect(
host=self.ip,
user=self.user,
passwd=self.password,
db=self.db,
charset='utf8mb4',
# unix_socket=self.socket

)

def __exit__(self, exc_type, exc_val, exc_tb):
self.con.close()
def data_to_csv(self,sql,st,en):
t = datetime.datetime.now()
file=t.strftime('取应收中转费用为空的数据_'+'%Y_%m_%d_%H_%M_%S')+'-'+st+'_'+ en +".csv"
tmp_file_name = '/data2/zt/zt2/0108/csv/cx/20220715/'+file
try:
with codecs.open(filename=tmp_file_name, mode='w', encoding='utf-8-sig') as f:
write = csv.writer(f, dialect='excel')
cursor=self.conn.cursor()
cursor.execute(sql)
results = cursor.fetchall()
result2 = []
stra = ''
for i in cursor.description:
result2.append(i[0])
write.writerow(result2)
for i in results:
dd = []
for j in i:
dd.append(str(j))
write.writerow(dd)
return tmp_file_name,file
except Exception as e:
print(e)
db=sys.argv[1]


#s = SQLgo(ip,user,password,db)
zs = 1000000
st = 0
en = 1000000

for limit_num in range(1,13):
s = SQLgo(ip,user,password,db)
sqll = "SELECT t.waybill_no,t.fee_cost ,t.update_time FROM yl_jms_spmi_transfer_bill_pt t LEFT JOIN tof_data_comparison p ON t.waybill_no = p.waybill_no WHERE t.is_delete = 1 AND t.waybill_no = p.waybill_no and t.fee_type_id = 158 AND t.fee_cost > 0 AND t.bill_generation_date >= '2022-01-01' order by t.waybill_no limit %s,%s" %(str(st),str(zs))
sqll = "SELECT p.send_network_name 寄件网点,p.waybill_no 运单编号,p.send_financial_center_name 寄件财务中心,p.start_financial_center_name 始发财务中心,p.package_charge_weight 计费重量,p.destination_name 目的地市,p.waybill_operation_time 运单录入时间,t.sign_network_name FROM yl_jms_spmi_transfer_bill_pt p LEFT JOIN yl_jms_spmi_piece_bill_pt t on p.waybill_no = t.waybill_no WHERE p.bill_generation_date BETWEEN '2022-03-01' AND '2022-03-31' AND p.fee_type_id = 168 AND p.is_delete = 1 AND p.waybill_no not in (SELECT waybill_no from yl_jms_spmi_transfer_bill_pt where bill_generation_date BETWEEN '2022-01-01' AND '2022-06-30' AND fee_type_id = 158 AND is_delete = 1 ) and t.fee_type_id = 201 order by p.waybill_no limit %s,%s" %(str(st),str(zs))
s.data_to_csv(sqll,str(st),str(en))
st = st + zs
en = en + zs

 

用法:

python3 脚本名 库名

 



这篇关于python脚本导数据-2-csv的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程