Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
4 Răspunsuri
6254 Vizualizări

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.??





Imagine profil
Abandonează

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

Autor

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

Autor Cel mai bun răspuns

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.


Imagine profil
Abandonează
Cel mai bun răspuns

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'))

Imagine profil
Abandonează
Autor

@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 Răspunsuri Vizualizări Activitate
0
ian. 20
3637
0
iun. 19
3324
0
mar. 15
3909
2
mar. 24
11896
1
mar. 23
2439