【问题标题】:Print only specific parts of json file仅打印 json 文件的特定部分
【发布时间】:2019-03-05 19:14:44
【问题描述】:

我想知道在 python 中尝试打印以下代码的名称数据时我做错了什么。

        import urllib.request, json 
    with urllib.request.urlopen("<THIS IS A URL IN THE ORIGINAL SCRIPT>") as url:
        data = json.loads(url.read().decode())
    print (data['Departure']['Product']['name'])
print (data['Departure']['Stops']['Stop'][0]['depTime'])

这是我从中获取数据的 api:

    {
  "Departure" : [ {
    "Product" : {
      "name" : "Länstrafik - Buss 201",
      "num" : "201",
      "catCode" : "7",
      "catOutS" : "BLT",
      "catOutL" : "Länstrafik - Buss",
      "operatorCode" : "254",
      "operator" : "JLT",
      "operatorUrl" : "http://www.jlt.se"
    },
    "Stops" : {
      "Stop" : [ {
        "name" : "Gislaved Lundåkerskolan",
        "id" : "740040260",
        "extId" : "740040260",
        "routeIdx" : 12,
        "lon" : 13.530096,
        "lat" : 57.298178,
        "depTime" : "20:55:00",
        "depDate" : "2019-03-05"
      }

【问题讨论】:

  • 欢迎来到 Stack Overflow!请正确缩进你的 python 代码,这样人们可以更好地理解你的代码,并且更能帮助你。

标签: python json api


【解决方案1】:

data["Departure"] 是一个列表,您正在对它进行索引,就像它是一本字典一样。

【讨论】:

    【解决方案2】:

    你写的字典样本很混乱。这是我认为的外观:

    d =     {
      "Departure" : [ {
        "Product" : {
          "name" : "Länstrafik - Buss 201",
          "num" : "201",
          "catCode" : "7",
          "catOutS" : "BLT",
          "catOutL" : "Länstrafik - Buss",
          "operatorCode" : "254",
          "operator" : "JLT",
          "operatorUrl" : "http://www.jlt.se"
        },
        "Stops" : {
          "Stop" : [ {
            "name" : "Gislaved Lundåkerskolan",
            "id" : "740040260",
            "extId" : "740040260",
            "routeIdx" : 12,
            "lon" : 13.530096,
            "lat" : 57.298178,
            "depTime" : "20:55:00",
            "depDate" : "2019-03-05"
          }]}}]}
    

    下面是如何打印depTime

    print(d["Departure"][0]["Stops"]["Stop"][0]["depTime"])
    

    您错过的重要部分是d["Departure"][0],因为d["Departure"]list

    【讨论】:

    • Traceback(最近一次调用最后一次):文件“C:/Users/Albin/Pythontest.py”,第 4 行,在 打印(data['Departure']['Product'] ['name']) TypeError: list indices must be integers or slices, not str
    • 我仍然得到这个错误代码,可能是什么问题?
    • @AlbinKarlsson 该错误代码表明您实际上并未进行我建议的更改。它说data['Departure']['Product']['name'] 而不是data["Departure"][0]["Stops"]["Stop"][0]["depTime"]
    • 好的,我明白了!非常感谢您的帮助!因为我看到你知道你在说什么。如果 json 文件中有另一个产品我也想打印,我该怎么办,例如 json 文件中的产品名称“Länstrafik - Buss 243”,有没有办法做到这一点?跨度>
    • 听起来像一个for循环,如for product_name in data["Product"]:
    【解决方案3】:

    正如凯尔在上一个答案中所说,data["Departure"] 是一个列表,但您正试图将其用作字典。有两种可能的解决方案。

    1. data["Departure"]["Stops"]["Stop"] 等更改为data["Departure"][0]["Stops"]["Stop"]

    2. 将 JSON 文件更改为字典,这样您就可以保留原始代码。这将使最终的 JSON sn-p 看起来像这样:

    "Departure" : {
      "Product" : {
        "name" : "Länstrafik - Buss 201",
        "num" : "201",
        "catCode" : "7",
        "catOutS" : "BLT",
        "catOutL" : "Länstrafik - Buss",
        "operatorCode" : "254",
        "operator" : "JLT",
        "operatorUrl" : "http://www.jlt.se"
      },
      "Stops" : {
        "name" : "Gislaved Lundåkerskolan",
        "id" : "740040260",
        "extId" : "740040260",
        "routeIdx" : 12,
        "lon" : 13.530096,
        "lat" : 57.298178,
        "depTime" : "20:55:00",
        "depDate" : "2019-03-05"
      }
    }
    

    【讨论】:

    • 我仍然收到此错误代码: Traceback(最近一次调用最后一次):文件“C:/Users/Albin/Pythontest.py”,第 4 行,在 打印(数据 [' Departure']['Product']['name']) TypeError: list indices must be integers or slices, not str
    • 嗯,好的。你能告诉我打印数据时它说什么吗?如print(data)
    猜你喜欢
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2017-09-24
    • 2023-03-28
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多