0

Pls help me. I have some API command result:

[{u'task': {u'url': u'http://192.168.1.1/job/f1111111111/', u'color': u'aborted', u'name': u'f1111111111'}, u'stuck': False, u'url': u'queue/item/37/', u'inQueueSince': 1397554800875L, u'actions': [{u'causes': [{u'userName': u'admin', u'userId': u'admin', u'shortDescription': u'Started by user admin'}]}], u'why': u'Waiting for next available executor on NODE_1', u'buildable': True, u'params': u'', u'buildableStartMilliseconds': 1397554800878L, u'id': 37, u'pending': False, u'blocked': False}, {u'task': {u'url': u'http://192.168.1.1/job/1234/', u'color': u'aborted', u'name': u'1234'}, u'stuck': False, u'url': u'queue/item/36/', u'inQueueSince': 1397554797741L, u'actions': [{u'causes': [{u'userName': u'admin', u'userId': u'admin', u'shortDescription': u'Started by user admin'}]}], u'why': u'Waiting for next available executor on NODE_1', u'buildable': True, u'params': u'', u'buildableStartMilliseconds': 1397554797744L, u'id': 36, u'pending': False, u'blocked': False}]

How can I make grep using PYthon to have the next output:

u'name': u'f1111111111'

u'name': u'1234

aRTURIUS
  • 1,230
  • 2
  • 13
  • 31
  • Do you mean searching strings in python with regular expressions (aka import re), or calling something on the command line? – Adam Morris Apr 15 '14 at 10:27

2 Answers2

3

Normaly you use grep (or regular expression) only as last resort to get structure out of a string literal. In your case you already got a kind of structured result, thus you should be able to iterate over it. (= much faster parsing)

  for row in result:  #iterate over the result list
    # do something with the row
    print row["task"]["name"] #access particular key

The ideal would be to convert the result into a custom dictionary alike structure, where you can access the result properties directly e.g. row.task.name. How to get there is nicely explained here

Community
  • 1
  • 1
Martin Stancik
  • 339
  • 2
  • 4
2

You'd better loop through your string like this:

>>> s=[{u'task': {u'url': u'http://192.168.1.1/job/f1111111111/', u'color': u'aborted', u'name': u'f1111111111'}, u'stuck': False, u'url': u'queue/item/37/', u'inQueueSince': 1397554800875L, u'actions': [{u'causes': [{u'userName': u'admin', u'userId': u'admin', u'shortDescription': u'Started by user admin'}]}], u'why': u'Waiting for next available executor on NODE_1', u'buildable': True, u'params': u'', u'buildableStartMilliseconds': 1397554800878L, u'id': 37, u'pending': False, u'blocked': False}, {u'task': {u'url': u'http://192.168.1.1/job/1234/', u'color': u'aborted', u'name': u'1234'}, u'stuck': False, u'url': u'queue/item/36/', u'inQueueSince': 1397554797741L, u'actions': [{u'causes': [{u'userName': u'admin', u'userId': u'admin', u'shortDescription': u'Started by user admin'}]}], u'why': u'Waiting for next available executor on NODE_1', u'buildable': True, u'params': u'', u'buildableStartMilliseconds': 1397554797744L, u'id': 36, u'pending': False, u'blocked': False}]
>>> for i in s:
...     print i['task']['name']
... 
f1111111111
1234

You can store the information in a list:

>>> l=[]
>>> for i in s:
...     l.append(i['task']['name'])
... 
>>> 
>>> print l
[u'f1111111111', u'1234']
fedorqui
  • 275,237
  • 103
  • 548
  • 598