簡體   English   中英

從字典列表中的字典列表中獲取數據

[英]Get data from a list of dictionaries from a dictionary list

下午好,告訴我如何獲取工人數據?

他們的路徑是 poolList -> AdditionalInfo -> Worker

這是我從 api 得到的結構:

parsUrlMi = {
      "id": 1000000001,
      "name": "Antminer S17",
      "hostname": "192.168.0.224",
      "hostDescription": "192.168.0.224:4028",
      "groupID": 0,
      "pool": "stratum.slushpool.com",
      "temperature": "59 59 59 °C",
      "hardware": "AntminerS17",
      "softwareType": "CgMinerCustom",
      "macAddress": "A1B2C3D4E5F6",
      "statusInfo": {
        "statusDisplay": "Mining",
        "statusLine3": "1h 41m ",
        "secondsSinceStart": 6087
      },
      "progressInfo": {
        "line1": "Accepted: 5442",
        "line2": "Rejected: 4",
        "line3": "HW Errors: 4",
        "acceptedValue": 5442,
        "rejectedValue": 4,
        "hwErrorsValue": 4
      },
      "speedInfo": {
        "logInterval": 5,
        "hashrate": "73,22 TH/s",
        "hashrateValue": 73220000000000,
        "avgHashrate": "73,96 TH/s",
        "avgHashrateValue": 73959000000000,
        "workUtility": "0,00",
        "line2": "Avg: 73,96 TH/s",
        "line3": "Power: 2865 W"
      },
      "coinInfo": {
        "displayName": "Bitcoin (BTC)",
        "shortName": "BTC",
        "revenuePerDay": "$10,17",
        "revenuePerDayValueDisplayCurrency": 10.165515374422721,
        "revenuePerDayValue": 10.165515374422721,
        "revenuePerDayValueBtc": 0.0011888546144192885,
        "revenuePerMonth": "$308,02",
        "profitPerDayValue": 10.165515374422721,
        "profitPerDayValueBtc": 0.0011888546144192885,
        "profitPerDay": "$10,17",
        "profitPerMonth": "$308,02",
        "powerUsageValue": 2865,
        "isActualPowerUsage": true,
        "algorithm": "SHA256",
        "coinClass": "PreDefined"
      },
      "maxTemperatureValue": 59,
      "avgTemperatureValue": 59,
      "isRunning": true,
      "isOffline": false,
      "updatedUtc": "2020-03-01T17:08:28.9865477Z",
      "updated": "2020-03-01 18:08:28",
      "poolList": [
        {
          "id": 0,
          "name": "awesome.1@stratum.slushpool.com",
          "statusInfo": {
            "statusDisplay": "Enabled"
          },
          "additionalInfo": {
            "displayUrl": "stratum.slushpool.com:3333",
            "worker": "awesome.1"
          },
          "priorityInfo": {
            "priority": 0,
            "quota": 1
          },
          "progressInfo": {
            "line1": "5442",
            "line2": "4",
            "acceptedValue": 5442,
            "rejectedValue": 4,
            "hwErrorsValue": 0
          }
        }
      ]
    }
.....

我的代碼:

Awesome_1 = []
  
for i in parsUrlMi:
        r = requests.get(i)
        data = json.loads(r.text)
            for _minerList in _groupList["minerList"]:
                dict_miner = {}
                dict_miner['Object'] = name_object
                dict_miner['subobject'] = _groupList["name"]
                dict_miner['Name'] = _minerList['name']
                dict_miner['Ip'] = _minerList['hostDescription']
                dict_miner['Worker'] = _minerList['poolList']['additionalInfo'] ['Worker']
                dict_miner['Status'] = _minerList['statusInfo']['statusDisplay']
                dict_miner['Mac'] = _minerList.get('macAddress')
    
                Awesome_1.append(dict_miner)

告訴我在我的代碼中需要做什么才能使其工作? 錯誤:TypeError:列表索引必須是整數或切片,而不是 str

我想得到一個結果:

Awesome_1 =  [{
  "Object": "some data",
  "subobject": "some data",
  "Name": "Antminer S17",
  "Ip": "192.168.0.224:4028",
  "Status": "Майнит",
  "Mac": null
  "Worker": awesome.1

}]

我對數據結構感到困惑,提前謝謝你,如果你決定提供幫助,謝謝

如果您仔細觀察,您會發現“poolList”是一個字典列表。 這就是您的錯誤的來源。

像這樣的東西應該工作

dict_miner['Worker'] = _minerList['poolList'][0]['additionalInfo']['Worker']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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