簡體   English   中英

不明白為什么會出現這個 NameError: name 'null' is not defined 異常

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

我正在測試新代碼,但出現以下錯誤。 如果我將字段“名稱:null”更改為“名稱: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)

null不是 Python 中的關鍵字。 您應該將其設為字符串(例如'name': 'null' )或使用None代替

我修改了腳本以使用 json URL 而不是將數據直接添加到腳本中,然后它運行良好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM