簡體   English   中英

試圖 append 列出多個項目,但得到 Python 類型錯誤:列表索引必須是整數或切片,而不是 str

[英]Trying to append multiple items to list but getting Python TypeError: list indices must be integers or slices, not str

我正在嘗試遍歷 customfield_10003 和 append 列表中每個問題的每個 customfield_10003 中的所有“名稱”值。

這是我正在研究的 JSON:

{
        "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
        "fields": {
            "customfield_10003": [
                {
                    "boardId": 11,
                    "completeDate": "2021-03-24T16:16:31.040Z",
                    "endDate": "2021-03-23T14:38:00.000Z",
                    "goal": "",
                    "id": 34,
                    "name": "Promo Sim 3/23",
                    "startDate": "2021-03-10T14:38:04.147Z",
                    "state": "closed"
                },
                {
                    "boardId": 11,
                    "completeDate": "2021-04-07T14:28:50.786Z",
                    "endDate": "2021-04-06T16:33:00.000Z",
                    "goal": "User will be able to create a new promotion simulator and include simulation inputs, select a simulation type. User will be able to view simulation inputs in the \"location\" they selected. User will be able to view predictors from a model on the Predictors tab. User will be able to view simulation inputs on the Spend tab.",
                    "id": 38,
                    "name": "Promo Sim 3",
                    "startDate": "2021-03-24T16:33:56.044Z",
                    "state": "closed"
                },
                {
                    "boardId": 11,
                    "completeDate": "2021-04-21T13:04:46.984Z",
                    "endDate": "2021-04-20T22:26:00.000Z",
                    "goal": "Ability to Publish a Predictive Model, Ability to calculate promo sim metrics, View Summary Results with calced metrics (using the predictor data inserted into backend**)",
                    "id": 39,
                    "name": "Promo Sim 4",
                    "startDate": "2021-04-07T12:12:30.195Z",
                    "state": "closed"
                }
            ],
            "summary": "Simulation Type - Fix \"Remove\" button display & Update \"Volume Components\" to \"Simulation Input\""
        },
        "id": "25391",
        "key": "PA-4699",
        "self": "N/A"
    }

這是我編寫的 Python 代碼,目前僅獲取列表中 [0] customfield_10003 的“名稱”值。 我不確定如何遍歷列表和 append 每個“名稱”值到問題列表 output。

issue_list = []
for issue in issues:
  if issue['fields']["customfield_10003"]:
    issue_list.append([issue['key'],issue['fields']['summary'],issue["fields"] 
    ["customfield_10003"][0]["name"]])
else:
  issue_list.append([issue['key'],issue['fields']['summary']])enter code here

如果您能更好地描述您想要的 output,那么可以提供更好的答案。

這將遍歷您的 json object 並將名稱拉入列表

#data = your sample json
data1 = data['fields']['customfield_10003']
names_list = [d['name'] for d in data1]

['Promo Sim 3/23', 'Promo Sim 3', 'Promo Sim 4']

暫無
暫無

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

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