嵌套的 OrderedDict 到具有不同嵌套级别的嵌套列表
Nested OrderedDict to nested list with varying levels of nesting
我正在使用 simple_salesforce
查询 CRM 关系数据库。使用 SOQL
查询数据,这是 SQL
的变体,专门用于 Salesforce。
它returns嵌套有序字典。上一个"join",如果没有记录,就returnsNone
。如果有记录,则返回 OrderedDict
。我的循环似乎不是最有效的,因为它重复了几行。我正在寻找一种更简洁的方式来编写循环。
使用的查询:
select Id, Subject, Account.Name, Opportunity.Name From Case where ID = '500xxxxxxxxxxxxx'
代码:
from collections import OrderedDict
od = OrderedDict([
(u'totalSize', 1),
(u'done', True),
(u'records', [OrderedDict([
(u'attributes', OrderedDict([
(u'type', u'Case'),
(u'url', u'/services/data/v29.0/sobjects/Case/500xxxxxxxxxxxxxxxx')
])
),
(u'Id', u'500xxxxxxxxxxxxxxxx'),
(u'Subject', u"Sample Case 1"),
(u'Account', OrderedDict([
(u'attributes', OrderedDict([
(u'type', u'Account'),
(u'url', u'/services/data/v29.0/sobjects/Account/001xxxxxxxxxxxxxxxx')
])
),
(u'Name', u'Sample Account 1')
])),
(u'Opportunity', None)]
)])])
output = []
def get_items(this_item):
for key, value in this_item.iteritems():
if key <> 'attributes':
if type(value) is OrderedDict:
for key, value in value.iteritems():
if key <> 'attributes':
record.append(str(value))
else:
record.append(str(value))
for item in od['records']:
record = []
record.append(get_items(item))
output.append(record)
print output
输出:
[['500xxxxxxxxxxxxxxxx', 'Sample Case 1', 'Sample Account 1', 'None']]
试试这个:
def get_rec_vals(rec):
ret_list = []
for key,val in rec.items():
if key == 'attributes': continue
if isinstance(val, OrderedDict):
# recursive call
ret_list.extend(get_rec_vals(val))
else:
ret_list.append(val)
return ret_list
output = [get_rec_vals(rec) for rec in od['records']]
print(output)
输出:
[['500xxxxxxxxxxxxxxxx', 'Sample Case 1', 'Sample Account 1', None]]
PS我不确定它是否适用于多条记录,因此您需要至少提供两条记录...
我正在使用 simple_salesforce
查询 CRM 关系数据库。使用 SOQL
查询数据,这是 SQL
的变体,专门用于 Salesforce。
它returns嵌套有序字典。上一个"join",如果没有记录,就returnsNone
。如果有记录,则返回 OrderedDict
。我的循环似乎不是最有效的,因为它重复了几行。我正在寻找一种更简洁的方式来编写循环。
使用的查询:
select Id, Subject, Account.Name, Opportunity.Name From Case where ID = '500xxxxxxxxxxxxx'
代码:
from collections import OrderedDict
od = OrderedDict([
(u'totalSize', 1),
(u'done', True),
(u'records', [OrderedDict([
(u'attributes', OrderedDict([
(u'type', u'Case'),
(u'url', u'/services/data/v29.0/sobjects/Case/500xxxxxxxxxxxxxxxx')
])
),
(u'Id', u'500xxxxxxxxxxxxxxxx'),
(u'Subject', u"Sample Case 1"),
(u'Account', OrderedDict([
(u'attributes', OrderedDict([
(u'type', u'Account'),
(u'url', u'/services/data/v29.0/sobjects/Account/001xxxxxxxxxxxxxxxx')
])
),
(u'Name', u'Sample Account 1')
])),
(u'Opportunity', None)]
)])])
output = []
def get_items(this_item):
for key, value in this_item.iteritems():
if key <> 'attributes':
if type(value) is OrderedDict:
for key, value in value.iteritems():
if key <> 'attributes':
record.append(str(value))
else:
record.append(str(value))
for item in od['records']:
record = []
record.append(get_items(item))
output.append(record)
print output
输出:
[['500xxxxxxxxxxxxxxxx', 'Sample Case 1', 'Sample Account 1', 'None']]
试试这个:
def get_rec_vals(rec):
ret_list = []
for key,val in rec.items():
if key == 'attributes': continue
if isinstance(val, OrderedDict):
# recursive call
ret_list.extend(get_rec_vals(val))
else:
ret_list.append(val)
return ret_list
output = [get_rec_vals(rec) for rec in od['records']]
print(output)
输出:
[['500xxxxxxxxxxxxxxxx', 'Sample Case 1', 'Sample Account 1', None]]
PS我不确定它是否适用于多条记录,因此您需要至少提供两条记录...