当前位置: 首页 > news >正文

购物网站建设报价武汉网站建设公司

购物网站建设报价,武汉网站建设公司,期货软件定制开发公司,小米官网网站建设一、需求描述 1、从Mysql数据库表下载数据到服务器; 2、将数据已csv文件格式存储并对数据格式进行处理(添加表头,表头和数据均用竖线分隔符隔开,末尾也加分割符); 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.104'sftpPort = 22sftpUser = 'sftpsun'sftpPassord = 'hadoophadoop'sftpLocalDir = '/home/hadoop/python_file/' + datesftpRemoteDir = '/upload'xhDir = '/home/hadoop/xh/' + datexhFile = xhDir + '/' + 'xhFile.txt'# 序号if os.path.isdir(xhDir):if os.path.isfile(xhFile):with open(xhFile, 'r', encoding='utf-8') as f:xh = f.readline()xh = int(xh) + 1xh = str(xh).zfill(2)with open(xhFile, 'w', encoding='utf-8') as f:f.write(xh)else:with open(xhFile, 'w', encoding='utf-8') as f:f.write('00')else:os.makedirs(xhDir)with open(xhFile, 'w', encoding='utf-8') as f:f.write('00')with open(xhFile, 'r', encoding='utf-8') as f:xh = f.readline()sendFile = 'ZXSEND_0112_082_' + date + '_' + xh + '_001'sftpLocalFile = sftpLocalDir + '/' + sendFile# 目录创建if os.path.isdir(sftpLocalDir):passelse:os.makedirs(sftpLocalDir)# 数据下载conn = pymysql.connect(host="hadoop100",port=3306,user='root',password='Mysql123456#',database='flink_sql',charset='utf8')cursor = conn.cursor()sql = "select t.* from hot_item t"cursor.execute(sql)data = cursor.fetchall()print(data)cursor.close()# 数据格式处理写入本地文件header = ('w_end', 'item_id', 'item_count', 'rk')with open(sftpLocalFile + '.csv', "w+", newline="", encoding='utf-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',encoding='utf-8')with open(sftpLocalFile + '.csv', 'r') as f:for line in f:line = line.strip()if not line.endswith(r'|'):line += r'|'line += '\n'f2.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(username=sftpUser, password=sftpPassord)sftp = paramiko.SFTPClient.from_transport(sf)sftp_upload(sftp, sftpLocalDir, sftpRemoteDir)sf.close()
http://www.hkea.cn/news/660535/

相关文章:

  • 台州企业网站模板建站怎么在百度上做公司网页
  • 烟台网站建设联系企汇互联专业网站维护收费标准
  • 网络客户服务平台搜索优化推广公司
  • 建设网站技术方案线上教育培训机构十大排名
  • 沈阳人流seo优化师就业前景
  • 开发区网站制作公司seo关键词有话要多少钱
  • 网站被篡改处理app拉新平台
  • 在线房屋设计网站seo推广平台服务
  • 电子政务门户网站建设代码短链接生成网址
  • 崔各庄地区网站建设百度非企渠道开户
  • 怎么用自己的电脑做网站服务器产品推广平台排行榜
  • 中国做的比较好的电商网站有哪些哈市今日头条最新
  • 微信怎么做网站推广百度网站优化培训
  • 网站开发支持多个币种电子技术培训机构
  • 移动网站设计与制作怎么找关键词
  • 国内移动端网站做的最好的厦门人才网597人才网
  • 建网站收费吗aso关键词覆盖优化
  • 西安的网站设计与制作首页微信视频号怎么推广引流
  • 顺义公司建站多少钱pc端百度
  • wordpress收费资源下载关键词优化的策略
  • 广州做网站建设的公司网站公司
  • 做网络平台的网站有哪些广州网站维护
  • 网页 代码怎么做网站东莞市民最新疫情
  • 电子商务网站设计中影响客户体验的元素有搜索引擎有哪些种类
  • 网站建设难点优化关键词技巧
  • 免费行情网站链接百度知道合伙人官网
  • 餐饮公司网站建设的特点大数据智能营销
  • 济南快速排名刷关键词排名seo软件
  • 系统做网站的地方百度推广登录后台登录入口
  • 集约化网站建设情况广告公司网站制作