Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4 Trả lời
6250 Lượt xem

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





Ảnh đại diện
Huỷ bỏ

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

Tác giả

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

Tác giả Câu trả lời hay nhất

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.


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

@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

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 1 20
3637
0
thg 6 19
3324
0
thg 3 15
3909
2
thg 3 24
11896
1
thg 3 23
2439