簡體   English   中英

我正在嘗試從 alpaca API 流式傳輸數據。 使用 json.dumps 給了我錯誤代碼:類型集的對象不是 JSON 可序列化的

[英]I'm trying to stream data from alpaca API. Using json.dumps gives me error code: Object of type set is not JSON serializable

代碼圖片

試過 json.stringify

問題出在這一行:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM