我有一个包含~600k部分的CSV,可以上传到我网站的库存中。但是,此CSV仅包含有限的信息。我们缺少定价和其他相关信息。要获取此信息,我需要向提供商的API发出请求,并将其添加到CSV中。目前,我已将此部分文件拆分为6个部分,并同时在每个文件上运行脚本。如果我运行一个脚本,则需要数小时。然而,如果我把它分开,它会更快。
过程:
我在想我可以为每个项目分配一个唯一的ID,让脚本请求该信息,返回到原始的csv,最后将信息放回原始文档中。
我如何利用系统的全部潜力来运行此脚本?
这是我到目前为止所得到的:
import csv
import zeep
wsdl = '#####'
client = zeep.Client(wsdl=wsdl)
def get_data():
with open('partfile.csv')as f:
parts = csv.reader(f, delimiter='|')
with open('results.csv' , 'w+') as outfile:
with open('inventory.csv', 'w+') as inventoryfile:
output = csv.writer(outfile, delimiter=',')
inventoryoutput = csv.writer(inventoryfile, delimiter=',')
inventoryoutput.writerow([
'ID',
'WarehouseNumber',
'WarehouseName',
'QuantityAvailable'
])
# Header Row
output.writerow([
'ID',
'Make',
'Part Number',
'Price',
'Dealer Price',
'Retail Price',
'List Price',
'Core Cost',
'Part Description',
'Is Discontinued',
'Is Dropship Only',
'Is Refrigerant',
'Is Oversize',
'Is Hazmat',
'Sub Parts',
'Cross Reference Parts',
'Log',
'Total Inventory'
])
itemId = 0
for row in parts:
try:
item = client.service.ExactPartLookup('#####', '#####', row[0], row[1])
if (item == None):
raise Exception('Item is None')
except:
write_error(row[1])
continue
item = item.PartInformation_v2[0]
totalInventory = 0
data = [
itemId,
item.Make,
item.PartNumber,
item.Price,
item.Dealer,
item.Retail,
item.List,
item.CoreCost,
item.PartDescription,
item.IsDiscontinued,
item.IsDropShipOnly,
item.IsRefrigerant,
item.IsOversize,
item.IsHazmat,
item.SubParts,
item.CrossReferenceParts,
item.Log
]
print(item.PartNumber)
if (item.Inventory != None):
inventory = item.Inventory.InventoryInformation_v2
iterator = 0
for i in inventory:
inventoryoutput.writerow([
itemId,
inventory[iterator].WarehouseNumber,
inventory[iterator].WarehouseName,
inventory[iterator].QuantityAvailable
])
totalInventory += inventory[iterator].QuantityAvailable
iterator += 1
data.append(totalInventory)
itemId += 1
output.writerow(data)
def write_error( partNumber ):
with open("errors.log", "a+") as errorfile:
errorfile.write("Error! Part Number: " + partNumber + "\n")
get_data()
如果我能提供更多信息,请告诉我。
谢谢!