Skip to Content
Menu
This question has been flagged
4 Replies
5390 Views

I am using python 2.7.X version and I need to display my result of list of dictionary in key order same as in my output


CREATE THE LIST OF DICTIONARY : 

 

for acc_data in acc_pool.browse(cr,uid,acc_ids_in):

for line in acc_data.invoice_line:

c+=1

lst_data2.append({

'SupplierName':acc_data.partner_id.name or '',

'SupplierBRN':acc_data.partner_id.com_reg_no1 or '',

'InvoiceDate':acc_data.date_invoice or '',

'InvoiceNumber':acc_data.number or '',

'ImportDeclarationNo':'',

'LineNumber':c,

'ProductDescription':line.product_id.name or '',

'PurchaseValueMYR':line.price_unit or 0.00,

'GSTValueMYR':'',

'TaxCode':line.invoice_line_tax_id.name or '',

'FCYCode':'',

'PurchaseFCY':'',

'GSTFCY':'',

})


RESULT ::

my result displaying from the ubuntu terminal

lst_data2 ==========>>>lst_data2 [{'ProductDescription': u'Ink Cartridge', 'SupplierBRN': '', 'ImportDeclarationNo': '', 'GSTValueMYR': '', 'SupplierName': u'Vicking Direct', 'GSTFCY': '', 'TaxCode': u'Purchase Tax 15.00%', 'InvoiceDate': '2015-03-24', 'FCYCode': '', 'PurchaseFCY': '', 'PurchaseValueMYR': 58.0, 'LineNumber': 1, 'InvoiceNumber': u'EXJ/2015/002'}, {'ProductDescription': u'Toner Cartridge', 'SupplierBRN': '', 'ImportDeclarationNo': '', 'GSTValueMYR': '', 'SupplierName': u'Vicking Direct', 'GSTFCY': '', 'TaxCode': u'OTAX X', 'InvoiceDate': '2015-03-24', 'FCYCode': '', 'PurchaseFCY': '', 'PurchaseValueMYR': 65.0, 'LineNumber': 2, 'InvoiceNumber': u'EXJ/2015/002'}]

hear u can easily see my key of order is different from in my result

My Question is that i need to set the all the keys from same as created in list of dictionary above from the list.

How can I set the same order of list of keys from in my result.??





Avatar
Discard

use ordered dict. instead of regular dict. , visit this link to know use of order dict, http://pymotw.com/2/collections/ordereddict.html

Author

@Mansi Kariya I have resolved that one from my side Be a Coooool ..... :) Thanks For Your Comment

Author Best Answer

Solution from the Above Question...

Just need to import the Following Python  Library for the code

from collections import OrderedDict

for acc_data in acc_pool.browse(cr,uid,acc_ids_in):

c=0

      for line in acc_data.invoice_line:

          if line.invoice_line_tax_id:

              c+=1

              lst_data2.append(OrderedDict([

                  ('SupplierName',acc_data.partner_id.name or ''),

                  ('InvoiceNumber',acc_data.number or ''),

                  ('ImportDeclarationNo',''),

                  ('LineNumber',c),

                  ('ProductDescription',line.product_id.name or ''),

                  ('PurchaseValueMYR',line.price_unit or 0.00),

                  ('GSTValueMYR',''),

                  ('TaxCode',line.invoice_line_tax_id.name or ''),

                  ('FCYCode',''),

                  ('PurchaseFCY',''),

                  ('GSTFCY',''),

                  ]))

OrderedDict is a dictionary subclass that remembers the order in which its contents are added.

I my case the OrderedDict is the part of the python Collection Type Which is used to set the Order of the keys from the dictionary.


Avatar
Discard
Best Answer

A built in function "itemgetter" exists to sort/order a Dictionary by specifying its item-key

Example:

list_to_be_sorted = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]

from operator import itemgetter

newlist = sorted(list_to_be_sorted, key=itemgetter('name'))

Avatar
Discard
Author

@deep I doen't want to sort the keys of the dictionary I resolved that one from my side see below answer Thanks for your reply

Related Posts Replies Views Activity
0
Jan 20
2663
0
Jun 19
2468
0
Mar 15
3208
2
Mar 24
10327
1
Mar 23
1417