Commit e047253a by 岳巧源

add append function

parent 902d0b56
Showing with 33 additions and 7 deletions
import copy import copy
import re import re
import json import json
import openpyxl
import pandas as pd import pandas as pd
import requests import requests
import xlrd
from xlutils.copy import copy
url = "http://172.18.137.46:9211/liexin_all_sku/_search" url = "http://172.18.137.46:9211/liexin_all_sku/_search"
...@@ -81,7 +85,7 @@ def main(): ...@@ -81,7 +85,7 @@ def main():
ans = [] ans = []
result = [] result = []
for i in range(len(data)): for i in range(len(data)):
if i >= 1000: if i > 9:
break # for debug, don't forget to delete this line. break # for debug, don't forget to delete this line.
sku_name_str = data[i][0] sku_name_str = data[i][0]
res_tmp = match_sku(sku_name_str) res_tmp = match_sku(sku_name_str)
...@@ -157,6 +161,7 @@ def process(query_str: dict): ...@@ -157,6 +161,7 @@ def process(query_str: dict):
return {} return {}
# 覆盖写
def write_to_xlsx(info_map_list: list): def write_to_xlsx(info_map_list: list):
table = {'goods_name': [], 'goods_id': [], 'brand_name': []} table = {'goods_name': [], 'goods_id': [], 'brand_name': []}
for i in range(len(info_map_list)): for i in range(len(info_map_list)):
...@@ -169,13 +174,34 @@ def write_to_xlsx(info_map_list: list): ...@@ -169,13 +174,34 @@ def write_to_xlsx(info_map_list: list):
table["goods_id"].append('') table["goods_id"].append('')
table["brand_name"].append('') table["brand_name"].append('')
df = pd.DataFrame(table) df = pd.DataFrame(table)
df.to_excel('table.xlsx', sheet_name='Sheet1', index=False, startcol=3) df.to_excel('ans.xlsx', sheet_name='Sheet1', index=False, startcol=3)
# 追加写
def write_to_xlsx_append(info_map_list: list):
info_map = {'goods_name': [], 'goods_id': [], 'brand_name': []}
for i in range(len(info_map_list)):
if len(info_map_list[i]) != 0:
info_map["goods_name"].append(info_map_list[i]["goods_name"])
info_map["goods_id"].append(str(info_map_list[i]["goods_id"]))
info_map["brand_name"].append(info_map_list[i]["brand_name"])
else:
info_map["goods_name"].append('')
info_map["goods_id"].append('')
info_map["brand_name"].append('')
data = openpyxl.load_workbook('table.xlsx')
table = data[data.sheetnames[0]]
table.cell(1, 4).value = 'goods_name'
table.cell(1, 5).value = 'goods_id'
table.cell(1, 6).value = 'brand_name'
for i in range(2, len(info_map_list)+2):
table.cell(i, 4).value = info_map['goods_name'][i-2]
table.cell(i, 5).value = info_map['goods_id'][i-2]
table.cell(i, 6).value = info_map['brand_name'][i-2]
data.save('table.xlsx')
if __name__ == '__main__': if __name__ == '__main__':
res = main() res = main()
# this is how to write to xlsx file, don't delete it !! write_to_xlsx_append(res)
# df = pd.DataFrame({'One': ['', 'test123', ''], 'Two': ['qq', 'mr', 'asd']}) \ No newline at end of file
# df.to_excel('ans.xlsx', sheet_name='Sheet1', index=False, startcol=3)
write_to_xlsx(res)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment