gpt4 book ai didi

python - 如何解析Smartsheet API 2.0 Python SDK错误结果?

转载 作者:太空宇宙 更新时间:2023-11-03 15:48:38 25 4
gpt4 key购买 nike

我正在尝试让 update_rows 方法正常工作

(这里的答案还没有帮助:

cannot update row using Smartsheet API

)

并且想要捕获并解析结果

results = smartsheet.Sheets.update_rows(test_sheet_id, [row])

print(results)

给了我这个:

{"requestResponse": null, "result": {"shouldRetry": false, "name":
"InvalidRowLocationError", "code": 1062, "recommendation": "Do not retry
without fixing the problem.", "message": "Invalid row location.",
"statusCode": 400}}

请注意,成功看起来像这样(剪掉了大部分内容):

{"resultCode": 0, "message": "SUCCESS", "version": 21, "result":
[{"discussions": [], "createdAt": null, "above": null, "modifiedAt":
null, "columns": [], "toTop": null, "sheetId": null, "siblingId":
4800885606901636, "permalink": null, "id": 6067523002099588,
"accessLevel": null, "conditionalFormat": null, "attachments": [],
"cells": [{"columnType": null, "displayValue": null, "linksOutToCells":
null, "strict": true, "hyperlink": null, "formula": null, "format": null,
"conditionalFormat": null, "columnId": 7600931584927620, "linkInFromCell":
null, "value": null}, {"columnType": null, "displayValue": null, "
... snip ...

这看起来像一本字典,但键、项目、值无法识别。接下来它看起来像 json - 但我尝试过的任何方法(我对 json 还不太了解)都不起作用。

如果我能从成功中获得resultCode,那将是一个开始。更好的是来自结果的值,但这似乎是失败的字典和成功的列表。

我很困惑。任何帮助表示赞赏。我正在使用 Python 3.5、Smartsheet API 2.0 Python SDK

克雷格

最佳答案

我明白了。

results = smartsheet.Sheets.update_rows(test_sheet_id, [row])

从 SDK 的 models\error_result.py 代码中返回一个结果对象。

该对象有两个感兴趣的方法,每个属性都可以像这样引用:

print(results.result.code)

返回代码(例如1062)

这两个方法是 to_dictto_json,可以像这样访问和打印:

print(results.result.to_dict())

给出:

    {'shouldRetry': False, 'name': 'InvalidRowLocationError', 'code': 1062,
'recommendation': 'Do not retry without fixing the problem.', 'message':
'Invalid row location.', 'statusCode': 400}

my_dict = results.result.to_dict()
for key, value in my_dict.items():
print(key, value)

给出:

    shouldRetry False
name InvalidRowLocationError
code 1062
recommendation Do not retry without fixing the problem.
message Invalid row location.
statusCode 400

to_json代码

print(results.result.to_json())

给出

    {
"shouldRetry": false,
"name": "InvalidRowLocationError",
"code": 1062,
"recommendation": "Do not retry without fixing the problem.",
"message": "Invalid row location.",
"statusCode": 400
}

和:

my_json = results.result.to_json()
my_dict = json.loads(my_json)
for key, value in my_dict.items():
print(key, value)

给出:

    shouldRetry False
name InvalidRowLocationError
code 1062
recommendation Do not retry without fixing the problem.
message Invalid row location.
statusCode 400

关于python - 如何解析Smartsheet API 2.0 Python SDK错误结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41532188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com