想要导入、提取 Salesforce 报告内容到 pandas 然后保存
Want to import, extracted Salesforce report content into pandas and then save it
您好,我使用 simple_salesforce 模块连接到销售人员并将其定位到我想要提取的报告。此时此刻,我有所需的 csv 逗号分隔格式的数据,当我写的时候
d.content
它以逗号分隔显示数据,但我想在 pandas 数据框中有相同的数据。
d = session.get("https://nax.salesforce.com/xxxxxxxxxx?export=1&enc=UTF-8&xf=csv".format('xxxxxxxxxx'), headers=sf.headers, cookies={'sid': sf.session_id})
然后
in[22]: d.content
out[22]: "whole comma seperated data"
我希望以上数据在pandas或者保存在csv
当我写:
pd.DataFrame(d)
它给出了一个错误
PandasError: DataFrame constructor not properly called!
请告诉我如何进一步将以下数据保存在 csv 中或插入 pandas 以进一步保存。
服务器返回的数据包含在响应的 text
属性中:d.text
(参见文档 here)。
然后您可以使用答案 here 从字符串创建数据框:
from StringIO import StringIO
pd.DataFrame.from_csv(StringIO(d.text))
我知道这比您需要的更详细,但作为一个搜索了如何使用 python 将 salesforce 报告下载到 excel 的完整答案的人,我想分享我的答案:
from simple_salesforce import Salesforce
import pandas as pd
import requests
import io
# login info must be a string in "quotes"
sf = Salesforce(username= # "UserName"
,password= # "Password"
,security_token= # "Security Token" (can be reset in your settings in salesforce and will be sent via email)
)
sid=sf.session_id
print ("get sid "+ sf.session_id)
#ReportID can be found when you open the report in the browser address bar as follows: https://instance.salesforce.com/[Alphanumeric ReportID]
ReportID= #Alphanumeric Report ID as string in "quotes"
response = requests.get("https://instance.salesforce.com/"+ReportID+"?export=1&enc=UTF-8&xf=csv",
headers = sf.headers, cookies = {'sid' : sid})
df=pd.read_csv(io.StringIO(response.text))
df=df[:-5] #takes out the confidential information warning and copywrite info
#if your first column is a number field instead of string (text) field you will need the next line(Delete the '#')
#df=df.astype({"[first column name]":'float'})
writer = pd.ExcelWriter('Salesforce Output.xlsx') #this will write the file to the same folder where this program is kept
df.to_excel(writer,index=False,header=True)
writer.save()
我把评论写得非常简单,以便不经常使用的人python或初学者可以从我的经验中学习。
您好,我使用 simple_salesforce 模块连接到销售人员并将其定位到我想要提取的报告。此时此刻,我有所需的 csv 逗号分隔格式的数据,当我写的时候
d.content
它以逗号分隔显示数据,但我想在 pandas 数据框中有相同的数据。
d = session.get("https://nax.salesforce.com/xxxxxxxxxx?export=1&enc=UTF-8&xf=csv".format('xxxxxxxxxx'), headers=sf.headers, cookies={'sid': sf.session_id})
然后
in[22]: d.content
out[22]: "whole comma seperated data"
我希望以上数据在pandas或者保存在csv
当我写:
pd.DataFrame(d)
它给出了一个错误
PandasError: DataFrame constructor not properly called!
请告诉我如何进一步将以下数据保存在 csv 中或插入 pandas 以进一步保存。
服务器返回的数据包含在响应的 text
属性中:d.text
(参见文档 here)。
然后您可以使用答案 here 从字符串创建数据框:
from StringIO import StringIO
pd.DataFrame.from_csv(StringIO(d.text))
我知道这比您需要的更详细,但作为一个搜索了如何使用 python 将 salesforce 报告下载到 excel 的完整答案的人,我想分享我的答案:
from simple_salesforce import Salesforce
import pandas as pd
import requests
import io
# login info must be a string in "quotes"
sf = Salesforce(username= # "UserName"
,password= # "Password"
,security_token= # "Security Token" (can be reset in your settings in salesforce and will be sent via email)
)
sid=sf.session_id
print ("get sid "+ sf.session_id)
#ReportID can be found when you open the report in the browser address bar as follows: https://instance.salesforce.com/[Alphanumeric ReportID]
ReportID= #Alphanumeric Report ID as string in "quotes"
response = requests.get("https://instance.salesforce.com/"+ReportID+"?export=1&enc=UTF-8&xf=csv",
headers = sf.headers, cookies = {'sid' : sid})
df=pd.read_csv(io.StringIO(response.text))
df=df[:-5] #takes out the confidential information warning and copywrite info
#if your first column is a number field instead of string (text) field you will need the next line(Delete the '#')
#df=df.astype({"[first column name]":'float'})
writer = pd.ExcelWriter('Salesforce Output.xlsx') #this will write the file to the same folder where this program is kept
df.to_excel(writer,index=False,header=True)
writer.save()
我把评论写得非常简单,以便不经常使用的人python或初学者可以从我的经验中学习。