首页 > 解决方案 > 不明白为什么会出现这个 NameError: name 'null' is not defined 异常

问题描述

我正在测试一个新代码,我收到以下错误。如果我将字段“name:null”更改为“name:abc”之类的内容,它会起作用。任何想法?我如何跳过/解决这个“空”问题?

Traceback (most recent call last):
  File "<string>", line 3, in <module>
NameError: name 'null' is not defined

这是我的代码:

data = [{"id": 111,
         "description": "",
         "name": null,
         "name_with_namespace": "Zzzz",
         "path": "Zzzz"
         },
        {"id": 222,
         "description": "",
         "name": "xp-demo-gradle",
         "name_with_namespace": "xp-demo-gradle",
         "path": "xp-demo-gradle"
         }]
for request in data:
    lista = []
    request["id"]
    paragraph = "id: " + str(request["id"]) + "; Path: " + request["path"] + "; Name: " + request["name"]
    artifact = {
        "requested_by": "RequestFetcher",
        "argument": {
            "topic": {
                "id": 2
                },
            "key": str(request["id"]),
            "title": "ID " + str(request["id"]),
            "text": paragraph,
            "cached": True,
            "_links": [
                {
                    "href": "https://gitlab.local.com/api/v4/projects/" + str(request["name"]),
                    "rel": "self",
                    "method": "GET"
                    }
                ],
            }
        }
    lista.append(artifact)
    print(lista)

标签: pythonjsonpython-3.x

解决方案


null不是 Python 中的关键字。您应该将其设为字符串(例如'name': 'null')或None改用


推荐阅读