![](/img/trans.png)
[英]Python json.dumps TypeError: Object of type 'set' is not JSON serializable , when trying convert from variable , working when hardcoded
[英]I'm trying to stream data from alpaca API. Using json.dumps gives me error code: Object of type set is not JSON serializable
問題出在這一行:
auth_message = {"action": "auth", "key": {api_key}, "secret": {secret_key}}
在 Python 中, {something}
是創建集合的語法,它也可以有多個項目: {1, 2, 3}
是只包含 1、2 和 3 的集合。( 這里是 Python 中集合的文檔)。
您可能真正想要的是直接引用這些值,所以
auth_message = {"action": "auth", "key": api_key, "secret": secret_key}
我認為混淆可能來自這樣一個事實,即大括號用於將某些內容轉換為f"here curly braces do do what you probably thought they'd do here: {3 * 4}"
(這將等於"here curly braces do do what you probably thought they'd do here: it 12"
)或"the older way with .format also exists {}".format(3)
(將是"the older way with .format also exists 3"
) - 但這只是字符串插值的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.