我使用simple_salesforce
來查詢CRM關係數據庫。使用SOQL
查詢數據,這是SQL
專門針對Salesforce的變體。將嵌套的OrderedDict嵌套到不同級別的嵌套列表
它返回嵌套的有序字典。在「加入」中,如果沒有記錄,則返回None
。如果有記錄,則返回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']]
請張貼預期輸出 – MaxU
@MaxU剛剛發佈輸出和期望。謝謝。 – user2242044
只是檢查'值'的類型,不要打印它,如果它是'collections.OrderedDict' – MaxU