Created
August 30, 2015 10:51
-
-
Save zhangzhishan/f2630a8b48f7db4d25f5 to your computer and use it in GitHub Desktop.
A python script to compare some json data and the local excel files and save data everyday
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
# author:huhuhushan | |
from openpyxl import Workbook, load_workbook | |
import urllib2 | |
import os | |
import datetime | |
wb = load_workbook('/data/www/api12/new.xlsx') | |
ws = wb.active | |
wb_sum = load_workbook('/data/www/api12/sum.xlsx') | |
ws_sum = wb_sum.active | |
response = urllib2.urlopen('http://jsondata') | |
data = response.read() | |
# wbnew = Workbook() | |
# wsnew = wbnew.create_sheet() | |
# with open('1.txt') as f: | |
# data = f.read() | |
api_num = len(data.split(',')) | |
nowcol = ws_sum.max_column + 1 | |
ws_max_row = ws.max_row | |
for i in range(api_num): | |
api_sku = data.split(',')[i].split(':')[0].replace('\"', '').replace('{', '') | |
# print(api_sku) | |
api_stock = data.split(',')[i].split(':')[1].replace('\"', '').replace('}', '') | |
flag = 0 | |
for rownum in range(ws.min_row, ws_max_row + 1): | |
flag = flag + 1 | |
csv_id = ws.cell(row = rownum, column = 1).value | |
csv_sku = ws.cell(row = rownum, column = 2).value | |
# csv_stock = ws.cell(row = rownum, column = 3).value | |
# print(csv_sku) | |
if api_sku == csv_sku: | |
ws.cell(row = rownum, column = 3).value = api_stock | |
break | |
else: | |
if rownum == ws_max_row: | |
ws.append(['', api_sku, api_stock]) | |
filename = datetime.datetime.now().strftime('%Y%m%d') + '.xlsx' | |
wb.save('/data/www/api12/' + filename) | |
for rownum in range(ws_sum.min_row, ws_sum.max_row + 1): | |
ws_sum.cell(row = rownum, column = nowcol).value = ws.cell(row = rownum, column = 3).value | |
ws_sum.cell(row = 1, column = nowcol).value = datetime.datetime.now().strftime('%Y%m%d') + 'Stock' | |
wb_sum.save('/data/www/api12/sum.xlsx') | |
print '[+] ' + datetime.datetime.now().strftime('%Y%m%d') + ' ok!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment