我正在测试新代码,并且在下面出现错误。如果我将字段“名称: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)
答案 0 :(得分:3)
null
不是Python中的关键字。您应该将其设置为字符串(例如'name': 'null'
)或使用None
答案 1 :(得分:0)
我修改了脚本以使用json URL,而不是将数据直接添加到脚本中,然后效果很好。