我正在從 API 讀取字典,我希望能夠讀取鍵和值,通常我可以這樣做:fill['key'] 或 fill['created_at'] 但沒有任何作用。
for fill in last_fill:
key = fill
# key = str(fill)
print(f"key: {key}")
# value = fill["'" key "'"] # string indices must be integers
# value = fill["'",key,"'"]
for fill in last_fill:
print(fill)
# OUTPUTS all the KEYS
# created_at
# product_id
# order_id
# user_id
# ...
和這個:
for key in last_fill:
print(f"key: {key}") # OUTPUTS: fill: created_at
fill = last_fill[key]
print(fill["created_at"]) # and here the ERROR: string indices must be integers
uj5u.com熱心網友回復:
對字典執行for .. in ...回圈會回傳字典的鍵。所以你的fill變數代表每個鍵。
for key in last_fill:
fill = last_fill[key]
print(fill)
在您的情況下,您似乎實際上并不需要密鑰,因此您可以簡單地做
for fill in last_fill.values():
print(fill)
如果您需要鍵和值
for item in last_fill.items():
(key, value) = item
print(key, value)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/442809.html
標籤:Python python-3.x
上一篇:蟒蛇|啟動新執行緒,該執行緒將模擬另一個uid而不會影響主執行緒
下一篇:內部網路爬蟲庫
