python - 想要将Salesforce报表内容导入、提取到pandas中,然后保存

标签 python csv pandas salesforce

您好,我使用 simple_salesforce 模块连接到 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 中以进一步保存。

最佳答案

我知道这比您需要的更详细,但作为一个搜索了如何使用 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 的人或初学者可以从我的经验中学习。

关于python - 想要将Salesforce报表内容导入、提取到pandas中,然后保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36889098/

相关文章:

python - 有谁知道为什么 python dateutil 解析器告诉我它包含无效语法?昨晚还可以,现在就不行了

python - 可以跨 Python 子进程共享资源(日志记录、数据库、文件等)吗?

javascript - 从 Django 模型获取数据到 Google Charts

python - 执行 pandas groupby 操作的更快替代方案

python - 带有 DRF 错误用户名字段的 Djoser 是必需的

scala - 将Scala中的CSV读取到具有错误处理的案例类实例中

csv - 将 block 中的行转换为制表符分隔

node.js - 使用 node.js 和 node-csv-parser( Node 模块)提示 csv 文件下载为弹出窗口

python - 将列表拆分为单独的单词

python - Pandas to_sql 插入忽略