Hamed Haddadian
Hamed Haddadian

Reputation: 37

how can I print key and value form list of dictionaries in python

how can I print key and id from the following in python

[<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'>,
 ]

more information about the project. I am trying to use jira api calls and as an example get list of issues created by a certain user:

    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()

and my result looks like this [

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

I was wondering what kind of data I am getting in return. and how can I print key and id also maybe converting the result to a csv file.

Upvotes: 1

Views: 1738

Answers (2)

Danielle M.
Danielle M.

Reputation: 3662

import jira
# stuff

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

The result of the jira.search_issues function is a list of Jira objects. These objects are defined here: https://jira.readthedocs.io/en/latest/

If you want the whole object (every field) in JSON format:

print(issue.raw)

Upvotes: 2

Iman Mirzadeh
Iman Mirzadeh

Reputation: 13550

If you know key,values this is an easy way:

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

Upvotes: 1

Related Questions