Python为列表中的每个条目做些什么?

时间:2017-03-13 20:52:36

标签: python list trello

所以我一直在通过python与trello API进行交互。 当我拿到我的牌时,它会返回(其中包括)这个列表(将其转换为JSON以获得美观​​)

{
  "cards": [
    {
      "id": "censored",
      "checkItemStates": null,
      "closed": false,
      "dateLastActivity": "2017-03-13T20:31:15.161Z",
      "desc": "",
      "descData": null,
      "idBoard": "censored",
      "idList": "censored",
      "idMembersVoted": [

  ],
  "idShort": 1,
  "idAttachmentCover": null,
  "manualCoverAttachment": false,
  "idLabels": [

  ],
  "name": "testcard1",
  "pos": 65535,
  "shortLink": "censored",
  "badges": {
    "votes": 0,
    "viewingMemberVoted": false,
    "subscribed": false,
    "fogbugz": "",
    "checkItems": 0,
    "checkItemsChecked": 0,
    "comments": 0,
    "attachments": 0,
    "description": false,
    "due": null,
    "dueComplete": false
  },
  "dueComplete": false,
  "due": null,
  "email": "censored",
  "idChecklists": [

  ],
  "idMembers": [

  ],
  "labels": [

  ],
  "shortUrl": "censored",
  "subscribed": false,
  "url": "censored",
  "attachments": [

  ],
  "pluginData": [

  ]
}
  ]
}

我试过

for card in x.cards:
print "hi"

但它让我错误

AttributeError: 'list' object has no attribute 'cards'

我的终极目标是获取每个“名称”属性并将其打印在txt文件中(我知道如何将内容写入.txt文件)

在最后的结果中,将会有更多的卡片。

1 个答案:

答案 0 :(得分:1)

我建议您使用py-trello API从主板上检索数据。从卡上获取属性非常容易。参见下面的示例:

from trello import TrelloClient

def main():
    client = TrelloClient(
        api_key=TRELLO_API_KEY,
        api_secret=TRELLO_API_SECRET,
        token=TRELLO_OAUTH,
        token_secret=TRELLO_OAUTH_SECRET
    )

    boards = client.list_boards()

    for b in boards:
        print("\n# {}\n\n".format(b.name))
        print_board(b)

def print_board(board):
    lists = board.list_lists()
    for l in lists:
        print("\n## {}\n".format(l.name))
        print_list(l)

def print_list(lst):
    cards = lst.list_cards()
    for c in cards:
    print("* {}".format(c.name))


if __name__ == '__main__':
    main()

有关更多信息,请参见https://github.com/sarumont/py-trellohttps://github.com/sarumont/py-trello/issues/181 https://github.com/berezovskyi

的示例积分