Reputation: 301
I have a dict:
x = {'a': {'p': 'in-gaap', 'r': 'inr', 'dec': 0},
'b': {'p': 'in-ca', 'r': 'inr', 'dec': 0},
'c': {'p': 'in-gaap', 'r': '', 'dec': ''},
'd': {'p': 'in-gaap', 'r': 'inr', 'dec': 0}}
i am using this code to skip blank values:
for k,v in x.iteritems():
if d[k]['r'] == '' and d[k]['dec'] == '':
print k, x[k]['p']
else:
print x[k]['p'], x[k]['r'], x[k]['dec']
But the output is:
a,in-gaap
b,in-ca
c-in-gaap
d-in-gaap
it's not executing condition properly, it is printing all values
And I also tried using this code
for k,v in x.iteritems():
obj = re.match('\w+', x[k]['p'])
obj1 = re.match('\w+', x[k]['q'])
if not obj1 and obj2:
print k, x[k]['p']
else:
print x[k]['p'], x[k]['r'], x[k]['dec']
But output is same as above.
Actually the blank values of dict contains "\n"
, ''
, etc. and some non-ascii characters so I tried using re
, but it's not executing properly.
Upvotes: 0
Views: 1160
Reputation: 18028
Not sure if this is what you want:
for k,v in x.iteritems():
if not v['r'] and not v['dec']:
print k,v['p']
else:
print v['p'],v['r'],v['dec']
Outputs:
in-gaap inr 0
c in-gaap
in-ca inr 0
in-gaap inr 0
Upvotes: 0
Reputation: 6555
In your first code you are referring to a dict named d
, which we have no idea what it is. If I rename your dict to d
instead of x
, I get this output:
>>> ## working on region in file /usr/tmp/python-30572oWE...
in-gaap inr 0
c in-gaap
in-ca inr 0
in-gaap inr 0
Which seems to be what you are looking for.
Upvotes: 4