![](/img/trans.png)
[英]How to convert the extracted text from PDF to JSON or XML format in Python?
[英]How to format a table extracted from salesforce into python?
我能夠使用 python 從 salesforce 中提取一些字段。
我使用了以下代碼塊:
!pip install simple_salesforce
from simple_salesforce import Salesforce
import pandas as pd
sf = Salesforce(
username='',
password='',
security_token='')
sf_data = sf.query_all("SELECT Brand_Name__c,Name FROM AuthorisedProduct__c")
sf_df = pd.DataFrame(sf_data)
sf_df.head()
此過程將所有項目放在一個“記錄”字段中。
記錄 | 總尺寸 |
---|---|
OrderedDict([('attributes', OrderedDict([('type', 'AuthorisedProduct__c'), ('url', '/services/data/v42.0/sobjects/AuthorisedProduct__c/a020o00000xC1fmAAC')]), ('Brand_Name__c' ', 'ABB'), ('名稱', 'UNO-DM-1.2-TL-PLUS-B')]) | 14000 |
OrderedDict([('attributes', OrderedDict([('type', 'AuthorisedProduct__c'), ('url', '/services/data/v42.0/sobjects/AuthorisedProduct__c/a020o00000xC1fnAAC')]), ('Brand_Name__c' ', 'ABB'), ('名稱', 'UNO-DM-1.2-TL-PLUS-SB')]) | 14000 |
OrderedDict([('attributes', OrderedDict([('type', 'AuthorisedProduct__c'), ('url', '/services/data/v42.0/sobjects/AuthorisedProduct__c/a020o00000xC1foAAC')])), ('Brand_Name__c' ', 'ABB'), ('名稱', 'UNO-DM-2.0-TL-PLUS-B')]) | 14000 |
請注意,記錄下有 14000 個值。 我想在一個簡單的數據框中只有兩個字段。 帶有“Brand_Name__c”和“Name”字段的表。
品牌_名稱__C | 名稱 |
---|---|
ABB | UNO-DM-2.0-TL-PLUS-B |
ABB | UNO-DM-1.2-TL-PLUS-SB |
我們將有一個 14000 x 2 的矩陣。
請指教如何實現?
還有,如何扭轉這個過程?
非常感謝大家。
您可以在records
列中解壓OrderedDict
對象:
from collections import OrderedDict
import pandas as pd
df = pd.DataFrame({
'records':[
OrderedDict([('attributes', OrderedDict([('type', 'AuthorisedProduct__c'), ('url', '/services/data/v42.0/sobjects/AuthorisedProduct__c/a020o00000xC1fmAAC')])), ('Brand_Name__c', 'ABB'), ('Name', 'UNO-DM-1.2-TL-PLUS-B')]),
OrderedDict([('attributes', OrderedDict([('type', 'AuthorisedProduct__c'), ('url', '/services/data/v42.0/sobjects/AuthorisedProduct__c/a020o00000xC1fnAAC')])), ('Brand_Name__c', 'ABB'), ('Name', 'UNO-DM-1.2-TL-PLUS-SB')]),
OrderedDict([('attributes', OrderedDict([('type', 'AuthorisedProduct__c'), ('url', '/services/data/v42.0/sobjects/AuthorisedProduct__c/a020o00000xC1foAAC')])), ('Brand_Name__c', 'ABB'), ('Name', 'UNO-DM-2.0-TL-PLUS-B')])
],
'total size': [14000]*3
})
df['Brand_Name__c'] = df['records'].apply(lambda x: x['Brand_Name__c'])
df['Name'] = df['records'].apply(lambda x: x['Name'])
結果:
>>> df
records total size Brand_Name__c Name
0 {'attributes': {'type': 'AuthorisedProduct__c'... 14000 ABB UNO-DM-1.2-TL-PLUS-B
1 {'attributes': {'type': 'AuthorisedProduct__c'... 14000 ABB UNO-DM-1.2-TL-PLUS-SB
2 {'attributes': {'type': 'AuthorisedProduct__c'... 14000 ABB UNO-DM-2.0-TL-PLUS-B
您必須了解 Salesforce 發送的 JSON 響應的實際形狀,其中包括一個頂級"records"
鍵,您的所有數據都包含在該鍵下。 此外,除了您實際請求的字段的數據之外,每個記錄條目都包含一個"attributes"
鍵。 您無法更改 JSON 響應的形狀。
simple_salesforce
文檔中提供了一個示例,顯示了如何為 Pandas 消化此 API 響應:
從 SFDC API 查詢生成 Pandas 數據幀 (ex.query,query_all)
import pandas as pd
sf.query("SELECT Id, Email FROM Contact")
df = pd.DataFrame(data['records']).drop(['attributes'],axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.