将新的键值对添加到 AttributeDicts 列表中

Adding a new key value pair to a list of AttributeDicts

我正在努力在 for 循环中进行索引和更新。

我有以下 AttributeDicts 列表。

[
  {
    "blockHash": {},
    "blockNumber": 14262929,
    "contractAddress": null,
    "cumulativeGasUsed": 17784692,
    "effectiveGasPrice": 42175682624,
    "from": "0x603A9D861D793829F836f6C39c560836A71E7125",
    "gasUsed": 266567,
    "logs": [
      {
        "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x000000000000000000000000000000000000000000000000a688906bd8b00000",
        "logIndex": 319,
        "removed": false,
        "topics": [
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453
      },
      {
        "address": "0xa3AEe8BcE55BEeA1951EF834b99f3Ac60d1ABeeB",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x",
        "logIndex": 321,
        "removed": false,
        "topics": [
          {},
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453
      }
    ],
    "logsBloom": {},
    "status": 1,
    "to": "0x7f268357A8c2552623316e2562D90e642bB538E5",
    "transactionHash": {},
    "transactionIndex": 453,
    "type": "0x2"
  },
  {
    "blockHash": {},
    "blockNumber": 14262929,
    "contractAddress": null,
    "cumulativeGasUsed": 17784692,
    "effectiveGasPrice": 42175682624,
    "from": "0x603A9D861D793829F836f6C39c560836A71E7125",
    "gasUsed": 266567,
    "logs": [
      {
        "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x00000000000000000000000000000000000000000000000014d1120d7b160000",
        "logIndex": 320,
        "removed": false,
        "topics": [
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453
      },
      {
        "address": "0xa3AEe8BcE55BEeA1951EF834b99f3Ac60d1ABeeB",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x",
        "logIndex": 321,
        "removed": false,
        "topics": [
          {},
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453
      }
    ],
    "logsBloom": {},
    "status": 1,
    "to": "0x7f268357A8c2552623316e2562D90e642bB538E5",
    "transactionHash": {},
    "transactionIndex": 453,
    "type": "0x2"
  }
]

data != 0x 在嵌套字典中时,我想添加一个额外的 key:value 对。看起来像下面这样。

  {
    "blockHash": {},
    "blockNumber": 14262929,
    "contractAddress": null,
    "cumulativeGasUsed": 17784692,
    "effectiveGasPrice": 42175682624,
    "from": "0x603A9D861D793829F836f6C39c560836A71E7125",
    "gasUsed": 266567,
    "logs": [
      {
        "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x00000000000000000000000000000000000000000000000014d1120d7b160000",
        "logIndex": 320,
        "removed": false,
        "topics": [
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453,
        "Value": Decimal('1.5')
      },
      {
        "address": "0xa3AEe8BcE55BEeA1951EF834b99f3Ac60d1ABeeB",
        "blockHash": {},
        "blockNumber": 14262929,
        "data": "0x",
        "logIndex": 321,
        "removed": false,
        "topics": [
          {},
          {},
          {},
          {}
        ],
        "transactionHash": {},
        "transactionIndex": 453
      }
    ],
    "logsBloom": {},
    "status": 1,
    "to": "0x7f268357A8c2552623316e2562D90e642bB538E5",
    "transactionHash": {},
    "transactionIndex": 453,
    "type": "0x2"
  }

本质上,将 Value: Decimal(1.5) 添加到第一个嵌套字典的末尾。

如何用上述字典替换(或更新?)nodeTxLog中的原始字典?

我的代码

txDetails = []
for i in txHash:
      nodeTxLog = w3.eth.get_transaction_receipt(i)
      tx_log = json.dumps(nodeTxLog["logs"], sort_keys=True, indent=2, cls=TxEncoder)
      log_data = json.loads(tx_log)
      for d in log_data:
          if d["data"] != "0x":
              dl = d["data"]
              wei = w3.toInt(hexstr=dl)
              value = w3.fromWei(wei, 'ether')
              #How to replace (or update) the original Tx in nodeTxLog with d?
              d.update({"Value": value})
              #This doesn't work
              log_data.append(d)
              #This appends the original dict to the list, not the dict with updated key:value
              txDetails.append(nodeTxLog)

我不清楚你的数据来自哪里,但更新已粘贴的数据非常简单。
我想称它为嵌套数据结构,而不是你命名的字典
注意这里nullfalse要改成NoneFalse要改成运行。我想知道你是如何从 python print(some_data) 或直接从某些文件中复制的长数据结构?

blocks = [
    {
        "blockHash": {},
        "blockNumber": 14262929,
        "contractAddress": None,
        "cumulativeGasUsed": 17784692,
        "effectiveGasPrice": 42175682624,
        "from": "0x603A9D861D793829F836f6C39c560836A71E7125",
        "gasUsed": 266567,
        "logs": [
            {
                "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
                "blockHash": {},
                "blockNumber": 14262929,
                "data": "0x000000000000000000000000000000000000000000000000a688906bd8b00000",
                "logIndex": 319,
                "removed": False,
                "topics": [
                    {},
                    {},
                    {}
                ],
                "transactionHash": {},
                "transactionIndex": 453
            },
            {
                "address": "0xa3AEe8BcE55BEeA1951EF834b99f3Ac60d1ABeeB",
                "blockHash": {},
                "blockNumber": 14262929,
                "data": "0x",
                "logIndex": 321,
                "removed": False,
                "topics": [
                    {},
                    {},
                    {},
                    {}
                ],
                "transactionHash": {},
                "transactionIndex": 453
            }
        ],
        "logsBloom": {},
        "status": 1,
        "to": "0x7f268357A8c2552623316e2562D90e642bB538E5",
        "transactionHash": {},
        "transactionIndex": 453,
        "type": "0x2"
    },
    {
        "blockHash": {},
        "blockNumber": 14262929,
        "contractAddress": None,
        "cumulativeGasUsed": 17784692,
        "effectiveGasPrice": 42175682624,
        "from": "0x603A9D861D793829F836f6C39c560836A71E7125",
        "gasUsed": 266567,
        "logs": [
            {
                "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
                "blockHash": {},
                "blockNumber": 14262929,
                "data": "0x00000000000000000000000000000000000000000000000014d1120d7b160000",
                "logIndex": 320,
                "removed": False,
                "topics": [
                    {},
                    {},
                    {}
                ],
                "transactionHash": {},
                "transactionIndex": 453
            },
            {
                "address": "0xa3AEe8BcE55BEeA1951EF834b99f3Ac60d1ABeeB",
                "blockHash": {},
                "blockNumber": 14262929,
                "data": "0x",
                "logIndex": 321,
                "removed": False,
                "topics": [
                    {},
                    {},
                    {},
                    {}
                ],
                "transactionHash": {},
                "transactionIndex": 453
            }
        ],
        "logsBloom": {},
        "status": 1,
        "to": "0x7f268357A8c2552623316e2562D90e642bB538E5",
        "transactionHash": {},
        "transactionIndex": 453,
        "type": "0x2"
    }
]
# note null and false should be changed to None and False to run here
for block in blocks:
    for log in block["logs"]:
        if log["data"] != "0x":
            log["Value"] = "value_you_got_some_where"
print(blocks)  # check the output