# -*- coding:utf-8 -*- import json import openpyxl import xlrd import csv import os import time import sys import datetime import requests import re def traverse_dir(path): traverse_dir_data = [] for file in os.listdir(path): file_path = os.path.join(path, file) if os.path.isdir(file_path) == False: filename = file_path.split('.') lastindex = len(filename) - 1 if filename[lastindex] == 'xls' or filename[lastindex] == 'xlsx' or filename[lastindex] == 'csv': traverse_dir_data.append(file_path) return traverse_dir_data def show_time(): current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print("现在时间是: " + str(current_time)) def explain_excel(path, index=0): file_data = [] if path[-3:] == 'xls': workbook = xlrd.open_workbook(path) workbook_sheet = workbook.sheet_by_index(index) workbook_sheet_rows = workbook_sheet.nrows if workbook_sheet_rows > 0: for row_i in range(0, workbook_sheet_rows): if len(workbook_sheet.row_values(row_i)) > 0: file_data.append(workbook_sheet.row_values(row_i)) if path[-4:] == 'xlsx': workbook = openpyxl.load_workbook(path) for row_data in workbook.worksheets[index].values: if len(row_data) > 0: file_data.append(row_data) return file_data def explain_csv(path): file_data = [] if path[-3:] == 'csv': csv_reader = csv.reader(open(path)) for text in csv_reader: if len(text) > 0: file_data.append(text) return file_data # 解析并导出文件内容 def explain_file(path): file_data = [] path_data = path.split('.') lastindex = len(path_data)-1 if path_data[lastindex] == 'xls' or path_data[lastindex] == 'xlsx': file_data = explain_excel(path) if path_data[lastindex] == 'csv': file_data = explain_csv(path) return file_data def upload_data(excel_data,excel_i,filename): errfile = [] if len(excel_data) < 2: print('文件没有内容') else: excel_content = '' excel_obj = [] ed_i = 0 upload_count = 50 for ed in excel_data: if ed_i >= 0: excel_obj.append({ 'number': ed_i, 'tracking_no': ed[0], 'buyer_name': ed[1], 'buyer_mobile': ed[2], 'buyer_address': ed[3] }) excel_i = excel_i + 1 ed_i = ed_i + 1 if excel_i > 0: show_time() post_data = [] all_count = len(excel_obj) print('共有' + str(all_count) + '条记录待上传') for item in excel_obj: postdata_count = len(post_data) if postdata_count == upload_count: print('分批开始上传' + str(postdata_count) + '条数据') current_path = current_dir + '\\' + datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + '.json' print(current_path) f = open(current_path, 'w', encoding='utf-8') f.write(json.dumps(post_data, ensure_ascii=False)) f.close() url = 'http://lbb-chrome-api2.alible.cn/chrome/v3/batch_insert_order' files = {'order': open(current_path, 'rb')} r = requests.post(url, files=files, timeout=600) try: data = r.json() print(data['data']['result']) except: print(r.status_code) if r.status_code == 502: errfile.append(filename) all_count = all_count - upload_count print('分批上传结束') print('还剩下' + str(all_count) + '条数据待上传') post_data.clear() sys.stdout.flush() print('暂停10秒再上传') time.sleep(10) else: post_data.append(item) print('剩余'+str(all_count)+'条') if all_count > 0: print('分批开始上传' + str(all_count) + '条数据') current_path = current_dir + '\\' + datetime.datetime.now().strftime( '%Y-%m-%d-%H-%M-%S') + '.json' print(current_path) f = open(current_path, 'w', encoding='utf-8') f.write(json.dumps(post_data, ensure_ascii=False)) f.close() url = 'http://lbb-chrome-api2.alible.cn/chrome/v3/batch_insert_order' files = {'order': open(current_path, 'rb')} r = requests.post(url, files=files, timeout=600) try: data = r.json() print(data['data']['result']) except: print(r.status_code) if r.status_code == 502: errfile.append(filename) print('分批上传结束') else: print('no file to export') return errfile if __name__ == '__main__': os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'cacert.pem') # 当前路径 print('开始遍历目录') current_dir = os.path.abspath('.') file_path_data = traverse_dir(current_dir) finish = [] errorFile = [] for f in file_path_data: excel_i = 0 show_time() print('开始读取文件') print(f) excel_data = explain_file(f) _errorFile = upload_data(excel_data, excel_i, f) if len(_errorFile) > 0: errorFile.append(''.join(_errorFile)) finish.append(f) f_length = len(finish) print('已经传完'+str(f_length)+'个文件,包括'+str(finish)) if len(errorFile) > 0: print('出错文件,包括' + ''.join(errorFile)) print('暂停10秒钟,继续读取下一个文件') show_time() time.sleep(10) print('结束所有文件上传') show_time()