回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我试图使用python中的<code>simple_salesforce</code>包从salesforce查询信息</p>
<p>问题在于,它将作为父子关系一部分的字段嵌套到有序dict中的有序dict中</p>
<p>我想。。从Opportunity对象中查找id以及与该记录关联的accountid</p>
<p>SOQL查询可能看起来像</p>
<pre><code>query = "select id, account.id from opportunity where closedate = last_n_days:5"
</code></pre>
<p>在SOQL(salesforce对象查询语言)中,点表示数据库中的父子关系。因此,我试图从opportunity对象获取id,然后从该记录上的account对象获取相关id</p>
<p>由于某些原因,Id很好,但account.Id嵌套在有序dict中的有序dict中:</p>
<pre><code>q = sf.query_all(query)
</code></pre>
<p>这将收回一本有序字典</p>
<pre><code>OrderedDict([('totalSize', 455),
('done', True),
('records',
[OrderedDict([('attributes',
OrderedDict([('type', 'Opportunity'),
('url',
</code></pre>
<p>我将拉动<code>ordereddict</code>的<code>records</code>部分来创建一个<code>df</code></p>
<pre><code>df = pd.DataFrame(q['records'])
</code></pre>
<p>这给了我3列,一个有序dict称为<code>'attributes'</code>,<code>Id</code>,另一个有序dict称为<code>'Account'</code>。我正在寻找一种从嵌套有序dict<code>'Account'</code>中提取<code>('BillingCountry', 'United States')</code>片段的方法</p>
<pre><code>[OrderedDict([('attributes',
OrderedDict([('type', 'Opportunity'),
('url',
'/services/data/v34.0/sobjects/Opportunity/0061B003451RhZgiHHF')])),
('Id', '0061B003451RhZgiHHF'),
('Account',
OrderedDict([('attributes',
OrderedDict([('type', 'Account'),
('url',
'/services/data/v34.0/sobjects/Account/001304300MviPPF3Z')])),
('BillingCountry', 'United States')]))])
</code></pre>
<p>编辑:澄清我在寻找什么</p>
<p>我想以一个dataframe结束,每个查询字段都有一列</p>
<p>当我使用<code>df = pd.DataFrame(sf.query_all(query)['records'])</code>将<code>'records'</code>片段放入数据帧时,它给出了:</p>
<pre><code>attributes Id Account
OrderedDict([('type', 'Opportunity'), ('url', '/services/data/v34.0/sobjects/Opportunity/0061B003451RhZgiHHF')]) 0061B003451RhZgiHHF OrderedDict([('attributes', OrderedDict([('type', 'Account'), ('url', '/services/data/v34.0/sobjects/Account/0013000000MvkRQQAZ')])), ('BillingCountry', 'United States')])
OrderedDict([('type', 'Opportunity'), ('url', '/services/data/v34.0/sobjects/Opportunity/0061B00001Pa52QQAR')]) 0061B00001Pa52QQAR OrderedDict([('attributes', OrderedDict([('type', 'Account'), ('url', '/services/data/v34.0/sobjects/Account/0011300001vQPxqAAG')])), ('BillingCountry', 'United States')])
OrderedDict([('type', 'Opportunity'), ('url', '/services/data/v34.0/sobjects/Opportunity/0061B00001TRu5mQAD')]) 0061B00001TRu5mQAD OrderedDict([('attributes', OrderedDict([('type', 'Account'), ('url', '/services/data/v34.0/sobjects/Account/0011300001rfRTrAAE')])), ('BillingCountry', 'United States')])
</code></pre>
<p>删除<code>'attributes'</code>列后,我希望输出是</p>
<pre><code>Id BillingCountry
0061B003451RhZgiHHF 'United States'
0061B00001Pa52QQAR 'United States'
0061B00001TRu5mQAD 'United States'
</code></pre>