如何使用Beatbox Python API从Salesforce提取原始数据

3
我正在使用以下代码通过beatbox python API从Salesforce中提取数据。
import beatbox
sf_username = "xyz@salesforce.com"
sf_password = "123"
sf_api_token = "ABC"


def extract():
   sf_client = beatbox.PythonClient()
   password = str("%s%s" % (sf_password, sf_api_token))
   sf_client.login(sf_username, password)
   lead_qry = "SELECT CountryIsoCode__c,LastModifiedDate FROM Country limit 10"
   records = sf_client.query(lead_qry)
   output = open('output','w')
   for record in records:
     output.write('\t'.join(record.values())
   output.close()

if _name_ == '__main__':
 extract()

但这是我得到的输出。如何获取原始数据,只获取工作台中看到的值。我不想解析每种数据类型并获取原始值。

实际输出:

[{'LastModifiedDate': datetime.datetime(2012, 11, 2, 9, 32, 4), 'CountryIsoCode_c': 'AU', 'type': 'Country_c', 'Id': ''}, {'LastModifiedDate': datetime.datetime(2012, 8, 18, 14, 0, 21), 'CountryIsoCode_c': 'LX', 'type': 'Country_c', 'Id': ''}, {'LastModifiedDate': datetime.datetime(2012, 11, 12, 15, 20, 11), 'CountryIsoCode_c': 'AE', 'type': 'Country_c', 'Id': ''}, {'LastModifiedDate': datetime.datetime(2012, 11, 12, 15, 20, 29), 'CountryIsoCode_c': 'AR', 'type': 'Country_c', 'Id': ''}, {'LastModifiedDate': datetime.datetime(2012, 11, 2, 9, 32, 4), 'CountryIsoCode_c': 'AT', 'type': 'Country_c', 'Id': ''}, {'LastModifiedDate': datetime.datetime(2012, 11, 2, 9, 32, 4), 'CountryIsoCode_c': 'BE', 'type': 'Country_c', 'Id': ''}, {'LastModifiedDate': datetime.datetime(2012, 11, 12, 15, 21, 28), 'CountryIsoCode_c': 'BR', 'type': 'Country_c', 'Id': ''}, {'LastModifiedDate': datetime.datetime(2012, 11, 12, 15, 21, 42), 'CountryIsoCode_c': 'CA', 'type': 'Country_c', 'Id': ''}, {'LastModifiedDate': datetime.datetime(2012, 11, 12, 15, 36, 18), 'CountryIsoCode_c': 'CH', 'type': 'Country_c', 'Id': ''}, {'LastModifiedDate': datetime.datetime(2012, 11, 12, 15, 35, 8), 'CountryIsoCode_c': 'CL', 'type': 'Country_c', 'Id': ''}]

预期输出:

AU 2012-11-02T09:32:04Z
LX 2012-08-18T14:00:21Z

当您运行lead_qry时,Salesforce会返回一个sObject,而不仅仅是单个字段。我相信Workbench会为您解析。然而,sObject很容易解析成JSON,然后可以轻松访问以获取您要查找的原始值。为什么不这样做呢? - ZAR
1个回答

4
如果您处理表格数据,应该使用Pandas库。以下是一个示例:
import pandas as pd 
from datetime import datetime
import beatbox

service = beatbox.PythonClient()  
service.login('login_here', 'creds_here')


query_result = service.query("SELECT Name, Country, CreatedDate FROM Lead limit 5")  # CreatedDate is a datetime object
records = query_result['records']      # records is a list of dictionaries

records是一个字典列表,就像你之前提到的那样。

df = pd.DataFrame(records)
print (df)

     Country         CreatedDate Id              Name  type
0  United States 2011-05-26 23:39:58       qwe qwe      Lead
1         France 2011-09-01 08:45:26       qwe qwe      Lead
2         France 2011-09-01 08:37:36       qwe qwe      Lead
3         France 2011-09-01 08:46:38       qwe qwe      Lead
4         France 2011-09-01 08:46:57       qwe qwe      Lead

现在您拥有了表格样式的数据框架对象。您可以对多个列和行进行索引:
df['CreatedDate']

0   2011-05-26 23:39:58
1   2011-09-01 08:45:26
2   2011-09-01 08:37:36
3   2011-09-01 08:46:38
4   2011-09-01 08:46:57

以下是有关pandas时间功能的更多信息:http://pandas.pydata.org/pandas-docs/stable/timeseries.html

这里是有关pandas的信息:http://pandas.pydata.org/pandas-docs/stable/install.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接