【问题标题】:Real data elements from JSON来自 JSON 的真实数据元素
【发布时间】:2020-07-29 11:29:45
【问题描述】:

我正在尝试使用 python 从 json url 链接中提取数据元素。下面是代码。当我尝试提取元素时它正在部分工作

 response = urllib.request.urlopen(url)
    data = json.loads(response.read())
    print("planId",data[0]["planId"]) #Gives result as planId PWR93173MBE1
    print("postcode",data[0]["postcode"]) # Gives result as postcode 2000
    print("tariffType", data[0]["tariffType"]) This gives me error.

另外,如果我想提取费用中的PlanType等其他元素,我该怎么做呢?

   [
       {
          "planData":{
             "planType":"M",
             "tariffType":"SR",
             "contract":[
                {
                   "pricingModel":"SR",
                   "benefitPeriod":"Ongoing",
                   "coolingOffDays":10,
                   "additionalFeeInformation":"This offer provides access to wholesale prices, utilises your Powerbank to smooth wholesale market volatility and Powerwatch to warn of higher prices. For more information on this and any other standard fees, visit our website www.powerclub.com.au",
                   "fee":[
                      {
                         "description":"Annual Membership payable each year for each of your business premises taking supply.",
                         "amount":79,
                         "feeType":"MBSF",
                         "percent":0,
                         "feeTerm":"A"
                      },
                      {
                         "description":"Cost for providing a paper bill",
                         "amount":2.5,
                         "feeType":"PBF",
                         "percent":0,
                         "feeTerm":"F"
                      },
                      {
                         "description":"Disconnection fee",
                         "amount":59.08,
                         "feeType":"DiscoF",
                         "percent":0,
                         "feeTerm":"F"
                      },
                      {
                         "description":"Reconnection Fee",
                         "amount":59.08,
                         "feeType":"RecoF",
                         "percent":0,
                         "feeTerm":"F"
                      },
                      {
                         "description":"Meter  Read - Requested by  Customer",
                         "amount":12.55,
                         "feeType":"OF",
                         "percent":0,
                         "feeTerm":"F"
                      }
                   ],
          "planId":"PWR93173MBE1",
          "planType":"E#B#PWR93173MBE1",
          "postcode":2000
       }
    ]

【问题讨论】:

  • 您能否详细说明您遇到的问题、期望的结果以及您需要从费用中提取什么?
  • 从费用中我想要描述和金额。我试图获取描述 print("Fees", data[0]['planData']['fee']['description']) 但它不起作用。希望我能够解释这个问题。

标签: python json web-crawler


【解决方案1】:

tariffType 属性位于 planData 属性内,因此您需要执行类似的操作

print("tariffType", data[0]["planData"]["tariffType"])

【讨论】:

  • 我尝试遵循相同的方法并尝试收取费用,我尝试了 print("Fees", data[0]['planData']['fee']['description']) 和print("Fees", data[0]['fee']) ,但它不起作用。我在这里做错了什么。
  • 同样,fee 属性嵌套在树的下方。为此,您需要data[0]['planData']['contract'][0]['fee']
【解决方案2】:

你忘了嵌套,正确的应该是:

print("tariffType", data[0]["planData"]["tariffType"])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2016-09-13
    • 2016-02-11
    • 2020-11-04
    • 2021-10-06
    • 1970-01-01
    相关资源
    最近更新 更多