简体   繁体   中英

Don't understand why this NameError: name 'null' is not defined exception occurs

I'm testing a new code and I'm getting the error below. If I change the field "name: null" for something like "name: abc" it works. Any idea? How I can skip/workaround this "null" issue?

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

Here's my code:

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)

null is not a keyword in Python. You should either make it a string (eg 'name': 'null' ) or use None instead

I modified the script to use the json URL instead of add the data directly into the script and then it worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM