如何在python中打印字典的键和值表单列表

时间:2016-08-30 18:43:32

标签: python python-2.7 dictionary

如何在python中打印以下键和id

[<JIRA Issue: key=u'OPS-22158', id=u'566935'>,
 <JIRA Issue: key=u'OPS-22135', id=u'566480'>,
 <JIRA Issue: key=u'OPS-22131', id=u'566361'>,
 <JIRA Issue: key=u'OPS-21850', id=u'561948'>,
 <JIRA Issue: key=u'OPS-20967', id=u'533908'>,
 ]

有关该项目的更多信息。 我正在尝试使用jira api调用,并作为示例获取某个用户创建的问题列表:

    from jira import JIRA
    from getpass import getpass
    from pprint import pprint 
    import csv

    def main():
        options = {
            'server': 'https://staging-jira.engsrv.mobileiron.com/',
        'verify': False
        }
        password = getpass()
        jira = JIRA(options, basic_auth=('hhaddadian', password))

        # Get the mutable application properties for this server (requires
        # jira-system-administrators permission)
        # props = jira.application_properties()

        # Find all issues reported by the admin
        issues = jira.search_issues('assignee=hhaddadian')
        pprint (issues) 
        for items in issues:
            print items 
if __name__ == "__main__":
    main()

我的结果看起来像这样 [

root@localhost ~]# python test.py 
Password: 
/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py:838: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/security.html
  InsecureRequestWarning)
/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py:838: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/security.html
  InsecureRequestWarning)
/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py:838: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/security.html
  InsecureRequestWarning)
[<JIRA Issue: key=u'OPS-22158', id=u'566935'>,
 <JIRA Issue: key=u'OPS-22135', id=u'566480'>,
 <JIRA Issue: key=u'OPS-22131', id=u'566361'>,
 <JIRA Issue: key=u'OPS-21850', id=u'561948'>,
 <JIRA Issue: key=u'OPS-20967', id=u'533908'>,
 ]
OPS-22158
OPS-22135
OPS-22131
OPS-21850
OPS-20967

我想知道我得到了什么样的数据。如何打印密钥和id也可以将结果转换为csv文件。

2 个答案:

答案 0 :(得分:2)

import jira
# stuff

for issue in jira.search_issues('assignee=hhaddadian'):
    print(issue.fields.project.key)

jira.search_issues函数的结果是Jira对象的列表。这些对象在此处定义:https://jira.readthedocs.io/en/latest/

如果您想要JSON格式的整个对象(每个字段):

print(issue.raw)

答案 1 :(得分:1)

如果您知道密钥,这是一种简单的方法:

In [2]: dict_list = [{'key':'iman','value':21} , {'key': 'hooman', 'value' : 22}] #list of dictionaries

In [3]: for dict in dict_list: #dict = a dictionary of list
   ...:     print dict['key'], dict['value'] #key,values
   ...:     
iman 21
hooman 22