我的代碼:排序鍵
abc = {
'Name':emp_name,
'Id no':emp_idno,
'Leave taken':k,
'Leave reqd':b,
'Total days of leave':total,
'Reason':reason
}
我得到的輸出爲:
id, name, total, reqason, leave taken, leave reqd
我所要的輸出是這個順序:
Name, id, leave taken, leave reqd, total, reason
我對此很感興趣,如果有人能幫助我,這將是非常棒的。
我的CSV
碼dl = {'Name':emp_name,'Id no':emp_idno, 'Leave taken':k, 'Leave
> reqd':b, 'Reason':reason}
>
>
> key_list = ['Name', 'Id no', 'Leave taken', 'Leave reqd', 'Reason'] abc = { 'Name':emp_name, 'Id no':emp_idno, 'Leave taken':k,
> 'Leave reqd':b, 'Reason':reason }
>
> for k in key_list:
> print abc[k] Lst.append(k)
>
> keys = Lst[0].keys() with open('employee.csv', 'wb') as output_file:
> dict_writer = csv.DictWriter(output_file, keys)
> dict_writer.writeheader()
> dict_writer.writerows(Lst)
的可能的複製[Python字典,如何保持鍵/值的聲明相同的順序?](http://stackoverflow.com/questions/1867861/python-dictionary-how-to-keep-keys -values-in-order-order-declared-declared) –