做电子元器件销售什么网站好,外贸自建站如何收款,南宁seo推广外包,做网站应该学什么语言一、需求描述
1、从Mysql数据库表下载数据到服务器#xff1b; 2、将数据已csv文件格式存储并对数据格式进行处理#xff08;添加表头#xff0c;表头和数据均用竖线分隔符隔开#xff0c;末尾也加分割符#xff09;#xff1b; 3、文件路径文件夹以天为单位#xff0c…一、需求描述
1、从Mysql数据库表下载数据到服务器 2、将数据已csv文件格式存储并对数据格式进行处理添加表头表头和数据均用竖线分隔符隔开末尾也加分割符 3、文件路径文件夹以天为单位文件名中含日期和序号序号记录相同文件在同一天重新下载传送的批次 3、将文件压缩成.gz格式 4、文件以Sftp方式传送到对方服务器固定位置。
二、Python 全代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import paramiko,csv
import os, sys, stat
import pymysql
import time, gzipdef create_dir(sftp, sftpRemoteDir):try:if stat.S_ISDIR(sftp.stat(sftpRemoteDir).st_mode):passexcept Exception as e:sftp.mkdir(sftpRemoteDir)def sftp_upload(sftp, sftpLocalDir, sftpRemoteDir):if os.path.isdir(sftpLocalDir):for file in os.listdir(sftpLocalDir):remoteDirTmp os.path.join(sftpRemoteDir, file)localDirTmp os.path.join(sftpLocalDir, file)if os.path.isdir(localDirTmp):create_dir(sftp, remoteDirTmp)sftp_upload(sftp, localDirTmp, remoteDirTmp)else:print(upload file:, sftpLocalDir)try:sftp.put(sftpLocalDir, sftpRemoteDir)except Exception as e:print(upload error:, e)if __name__ __main__:# 变量date time.strftime(%Y%m%d, time.localtime())sftpHost 192.168.220.104sftpPort 22sftpUser sftpsunsftpPassord hadoophadoopsftpLocalDir /home/hadoop/python_file/ datesftpRemoteDir /uploadxhDir /home/hadoop/xh/ datexhFile xhDir / xhFile.txt# 序号if os.path.isdir(xhDir):if os.path.isfile(xhFile):with open(xhFile, r, encodingutf-8) as f:xh f.readline()xh int(xh) 1xh str(xh).zfill(2)with open(xhFile, w, encodingutf-8) as f:f.write(xh)else:with open(xhFile, w, encodingutf-8) as f:f.write(00)else:os.makedirs(xhDir)with open(xhFile, w, encodingutf-8) as f:f.write(00)with open(xhFile, r, encodingutf-8) as f:xh f.readline()sendFile ZXSEND_0112_082_ date _ xh _001sftpLocalFile sftpLocalDir / sendFile# 目录创建if os.path.isdir(sftpLocalDir):passelse:os.makedirs(sftpLocalDir)# 数据下载conn pymysql.connect(hosthadoop100,port3306,userroot,passwordMysql123456#,databaseflink_sql,charsetutf8)cursor conn.cursor()sql select t.* from hot_item tcursor.execute(sql)data cursor.fetchall()print(data)cursor.close()# 数据格式处理写入本地文件header (w_end, item_id, item_count, rk)with open(sftpLocalFile .csv, w, newline, encodingutf-8) as f:lines csv.writer(f,delimiter|)lines.writerow(header)for line in data:a list(line)lines.writerow(line)f2 open(sftpLocalFile .dat, w,encodingutf-8)with open(sftpLocalFile .csv, r) as f:for line in f:line line.strip()if not line.endswith(r|):line r|line \nf2.write(line)f2.close()os.remove(sftpLocalFile .csv)# 压缩with open(sftpLocalFile .dat, rb) as f:data f.read()with gzip.open(sftpLocalFile .dat .gz, wb) as f:f.write(data)os.remove(sftpLocalFile .dat)# 文件上传sf paramiko.Transport((sftpHost, sftpPort))sf.connect(usernamesftpUser, passwordsftpPassord)sftp paramiko.SFTPClient.from_transport(sf)sftp_upload(sftp, sftpLocalDir, sftpRemoteDir)sf.close()
文章转载自: http://www.morning.sskkf.cn.gov.cn.sskkf.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.nlbw.cn.gov.cn.nlbw.cn http://www.morning.qnlbb.cn.gov.cn.qnlbb.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.rfgc.cn.gov.cn.rfgc.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.xhqr.cn.gov.cn.xhqr.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.hgscb.cn.gov.cn.hgscb.cn http://www.morning.trnl.cn.gov.cn.trnl.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.irqlul.cn.gov.cn.irqlul.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.tqxtx.cn.gov.cn.tqxtx.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.wtyqs.cn.gov.cn.wtyqs.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.kkwbw.cn.gov.cn.kkwbw.cn http://www.morning.bhpjc.cn.gov.cn.bhpjc.cn http://www.morning.xhlht.cn.gov.cn.xhlht.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.pwzzk.cn.gov.cn.pwzzk.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.gczqt.cn.gov.cn.gczqt.cn http://www.morning.rhmt.cn.gov.cn.rhmt.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.ydxg.cn.gov.cn.ydxg.cn http://www.morning.rbjf.cn.gov.cn.rbjf.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.zpxwg.cn.gov.cn.zpxwg.cn http://www.morning.ysqb.cn.gov.cn.ysqb.cn http://www.morning.fkwgk.cn.gov.cn.fkwgk.cn http://www.morning.cbndj.cn.gov.cn.cbndj.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.wqrdx.cn.gov.cn.wqrdx.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.cyjjp.cn.gov.cn.cyjjp.cn http://www.morning.nmfml.cn.gov.cn.nmfml.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.spdyl.cn.gov.cn.spdyl.cn http://www.morning.brld.cn.gov.cn.brld.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.yhxhq.cn.gov.cn.yhxhq.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.fbbpj.cn.gov.cn.fbbpj.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.xhlht.cn.gov.cn.xhlht.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.xcfmh.cn.gov.cn.xcfmh.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.txzmy.cn.gov.cn.txzmy.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.fsnhz.cn.gov.cn.fsnhz.cn http://www.morning.xhwty.cn.gov.cn.xhwty.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn