對于沒有標題的庫存提要,我需要將 json 轉換為 csv,并且我的轉換沒有問題,但列順序正在改變。有沒有辦法保持列順序?這是我所擁有的:
acmeInventory = Stock.objects.filter(product__type__in=(1, 9), status=1,
product__acmesells=True)
data=[]
for item in acmeInventory:
sku = item.product.sku
if sku == 'PROVTi-BK':
sku = 'VTi-BK'
if sku == 'PROVT-BK':
sku = 'VT-BK'
quantity = int(item.stock)
if quantity < 50:
quantity = 0
itemDict={
'Manufacturers Item #':sku,
'Quantity Available': str(quantity),
'Stocking Unit of Measure': 'EA',
'Date Available': str(today),
'Item Description': item.product.name,
}
data.append(itemDict)
print(data)
keys = data[0].keys()
with open('AcmeIAV220.csv','w', newline='') as output_file:
dict_writer = csv.DictWriter(output_file,keys)
dict_writer.writerows(data)
ftp = ftplib.FTP(host,user,password)
ftp.encoding = "utf-8"
with open('AcmeIAV220.csv', "rb") as file:
ftp.storbinary("STOR AcmeIAV220.csv", file)
ftp.dir()
uj5u.com熱心網友回復:
嘗試使用OrderedDict而不是 dict
這是一個示例:Python 中的 OrderedDict (GeeksforGeeks)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/475903.html
