简体   繁体   English

Soap API 数据到 Google Sheets

[英]Soap API data to Google Sheets

Good morning,早上好,

I have been having troubles getting soap API data into google sheets.我在将soap API 数据放入谷歌表时遇到了麻烦。 When i run the Soap request I get the data as shown in the image .当我运行 Soap 请求时,我得到如图所示的数据。 [output data][1] [输出数据][1]

Then i tried getting this data into a google sheets using different methods, unfortunately no solution so far has worked.然后我尝试使用不同的方法将这些数据放入谷歌表格中,不幸的是,到目前为止没有任何解决方案有效。 The solutions i have tried is pickling the data the setting it in a different file and pushing that file into google sheets.我尝试过的解决方案是腌制数据并将其设置在不同的文件中并将该文件推送到谷歌表格中。

The current solution I'm working on is setting the output_data in a pandas dataframe and pushing like that, this is the current code but this also doesn't seem to work.我正在研究的当前解决方案是在 Pandas 数据帧中设置 output_data 并像这样推送,这是当前的代码,但这似乎也不起作用。 I will only leave out the credentials to authenticate with the API.我只会省略使用 API 进行身份验证的凭据。

def pandas_to_sheets(pandas_df, sheet, clear = True):
    # Updates all values in a workbook to match a pandas dataframe
    if clear:
        sheet.clear()
    (row, col) = pandas_df.shape
    cells = sheet.range("A1:{}".format(gspread.utils.rowcol_to_a1(row + 1, col)))
    for cell, val in zip(cells, iter_pd(pandas_df)):
        cell.value = val
    sheet.update_cells(cells)

def iter_pd(df):
    for val in list(df.columns):
        yield val
    for row in df.values:
        for val in list(row):
            if pd.isna(val):
                yield ""
            else:
                yield val

optionsReportAffiliateSite = [ {'dateFrom' : '01-01-2020', }] 

client = Client(wsdl)
client.service.authenticate(username, password, sandbox, locale, demo)

testReportAffiliateSite = client.service.getReportAffiliateSite(idCampaigns,optionsReportCampaign )

input_dict = zeep.helpers.serialize_object(testReportAffiliateSite)
df = pd.DataFrame(input_dict)

affliatesite = pd.DataFrame(df.values.tolist())[0]
reportdata = pd.DataFrame(df.values.tolist())[1]

pd.json_normalize(affliatesite)
pd.json_normalize(reportdata)

pd.concat([pd.json_normalize(affliatesite), pd.json_normalize(reportdata).reindex(pd.json_normalize(affliatesite).index)], axis=1)
wks = gc.open_by_key('1uPdi2w_1TajnKNN8G3uahgrSHLAPnbAHtHSPeaZN3y0').sheet1
pandas_to_sheets(pd.concat([pd.json_normalize(affliatesite), pd.json_normalize(reportdata).reindex(pd.json_normalize(affliatesite).index)], axis=1), wks)

This gives me the error "TypeError: Object of type Decimal is not JSON serializable"这给了我错误“TypeError:Decimal 类型的对象不是 JSON 可序列化的”

Many thanks in advance.提前谢谢了。 [1]: https://i.stack.imgur.com/efOEN.png [1]: https : //i.stack.imgur.com/efOEN.png

The decimal values in the response you're getting cannot be serialized to JSON.您获得的响应中的十进制值无法序列化为 JSON。

Because of this, you should transform this decimal values to another type which can be serialized.因此,您应该将此十进制值转换为另一种可以序列化的类型。 For example, float.例如,浮动。 So you can do the following:因此,您可以执行以下操作:

  • Define this function, to check if an element is a decimal and return it converted to float:定义此函数,以检查元素是否为十进制并将其转换为浮点数:
def f(v):
    if isinstance(v, Decimal):
        return float(v)
    else:
        return v
  • Iterate through your list and call the previous function for every value in it, using this:遍历列表并为其中的每个值调用前一个函数,使用以下命令:
soapResponse = map(lambda el : {k: f(v) for k, v in el.items()}, soapResponse)

Note:笔记:

Import decimal via from decimal import Decimal .导入decimal通过from decimal import Decimal

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM