[英]Convert JSON to CSV and maintain column order
對於沒有標題的庫存提要,我需要將 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()
嘗試使用OrderedDict而不是 dict
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.