【问题标题】:KeyError: 'sites' when using JSON in PythonKeyError:在 Python 中使用 JSON 时出现“站点”
【发布时间】:2019-06-11 03:47:55
【问题描述】:

在 Python(使用 Flask)中使用 JSON 库时出现 KeyError 不是很懂。

KeyError: 'sites'

是我遇到的错误

JSON 代码:

{
  "sites": {
      "example-site": {
          "domains": [
              "example.pagehost.com",
              "127.0.0.1:5000"
          ],
          "pages": {
              "index/": "index.html"
          },
          "index": "index.html",
          "owner": "Bevan",
          "id": "example-site"
      }
   }
}

Python 代码:

global sites_json
fsite = ""
print("Finding site")
for site in sites_json['sites']:
    for domain in site['domains']:
        if domain == host:
            fsite = site
            print("Found site")
            break

侵权行似乎是 for site in sites_json['sites'] 请知道发生了什么的人帮忙吗?

【问题讨论】:

  • 请显示sites_json的类型:print(type(sites_json))

标签: python json flask


【解决方案1】:

经过一番折腾,我发现我是通过 python3 -m flask run 命令启动我的 Flask 应用程序,该命令跳过了我的 JSON 文件的初始化步骤。当我通过 python3 app.py 命令启动时,它工作正常!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 2021-08-02
    • 2018-02-25
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多