Python 要将提取的Salesforce报告内容导入pandas并保存吗

Python 要将提取的Salesforce报告内容导入pandas并保存吗,python,csv,pandas,salesforce,Python,Csv,Pandas,Salesforce,您好,我使用simple_salesforce模块连接到salesforce,并将其定向到我想要提取的报告。此时,我有了csv逗号分隔格式的所需数据,并且当我写入 d.content它以逗号分隔的形式显示数据,但我希望在数据框中有相同的数据 d = session.get("https://nax.salesforce.com/xxxxxxxxxx?export=1&enc=UTF-8&xf=csv".format('xxxxxxxxxx'), headers=sf.header

您好,我使用simple_salesforce模块连接到salesforce,并将其定向到我想要提取的报告。此时,我有了csv逗号分隔格式的所需数据,并且当我写入
d.content
它以逗号分隔的形式显示数据,但我希望在数据框中有相同的数据

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})
然后

我希望上述数据以熊猫或csv格式保存

当我写作时:

pd.DataFrame(d)
它给出了一个错误,如下所示

PandasError: DataFrame constructor not properly called!

请告诉我如何进一步将以下数据保存在csv中或插入pandas中以进一步保存。

服务器返回的数据包含在响应的
text
属性中:
d.text
(请参阅文档)

然后,您可以使用答案从字符串创建数据帧:

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的人或初学者可以从我的经验中学习。

d.content
字符串吗?可能是重复的否,因为数据是从sfdc报告中取出的,并且还不存在于任何csv文件中。“d.content”用于检查从Salesforcein获取的数据中的内容[55]是的,它工作得很好。感谢您的帮助和时间。欢迎使用Stack Overflow,并感谢您评论良好的答案。:)
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()