Commit 8277176b by 岳巧源

add new file

parent 4ae72b08
File mode changed
File mode changed
File mode changed
import csv
import json
import logging
logging.basicConfig(level=logging.INFO, filename='waldom_dollar.log', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('waldom_data_process_USD.py')
# Tianyang_InventoryFeed title字段含义
# Manufacturer ----------------> brand_name
# 库存相加 HIJK 列
# 加一个 multiple 列
#
title_map = {'Part Number', 'Manufacturer', 'Stock EMEA', 'Stock USA', 'Stock APAC', 'Stock CN', 'MOQ', 'Multiplier',
'Price Break Qty 1', 'Price Break Qty 2', 'Price Break Qty 3',
'Price Break Qty 4', 'Price Break Qty 5', 'Price Break Qty 6',
'Price Break Qty 7', 'Price Break Qty 8', 'Price Break Qty 9',
}
ladder_map = {
'Price Break Qty 1', 'Price Break Qty 2', 'Price Break Qty 3',
'Price Break Qty 4', 'Price Break Qty 5', 'Price Break Qty 6',
'Price Break Qty 7', 'Price Break Qty 8', 'Price Break Qty 9',
}
class HandleCSV:
def parse(self, path):
with open(path, encoding='gbk', errors='ignore') as f:
result = []
first_line = []
reader = csv.reader(f)
for index, row in enumerate(reader):
if index == 0:
first_line = row
flag = self.validate(first_line)
if not flag:
logger.error("文件 %s 格式错误" % path)
return []
else:
json_str = self.generate_json(row, first_line)
result.append(json_str)
print(json_str)
logger.info("文件 %s 已解析" % path)
return result
# 校验excel表头符合既定格式
def validate(self, arr: list) -> bool:
size = len(title_map)
count = 0
for i in range(len(arr)):
if arr[i] in title_map:
count += 1
return count == size
def generate_json(self, arr: list, title: list) ->str:
table = dict()
table['price_is_us'] = True
table['ladder_price'] = []
table['supplier_name'] = 'Waldom美金'
for i in range(len(title)):
ladder_item_map = dict()
if title[i] in title_map:
if title[i] == 'Part Number':
table['goods_name'] = arr[i]
elif title[i] == 'Manufacturer':
table['brand_name'] = arr[i]
elif title[i] == 'Stock EMEA' or \
title[i] == 'Stock USA' or \
title[i] == 'Stock APAC' or \
title[i] == 'Stock CN':
if len(str(arr[i])) != 0:
if 'stock' not in table:
table['stock'] = int(arr[i])
else:
table['stock'] += int(arr[i])
elif title[i] == 'MOQ':
table['moq'] = arr[i]
elif title[i] == 'Multiplier':
table['multiple'] = arr[i]
elif title[i] in ladder_map:
if len(str(arr[i]).strip()) != 0:
ladder_item_map['purchases'] = int(arr[i])
ladder_item_map['price_us'] = float(arr[i+1])
ladder_item_map['price_cn'] = float(0)
table['ladder_price'].append(ladder_item_map)
return json.dumps(table, ensure_ascii=False)
if __name__ == '__main__':
result = HandleCSV().parse('Tianyang_InventoryFeed.csv')
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