Created
April 27, 2015 14:59
-
-
Save xcombelle/1dcfbb50a62aa4d94194 to your computer and use it in GitHub Desktop.
fixup for chat.python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pprint | |
d=[ | |
{ | |
"exchange": "B", | |
"price": { | |
"id": 1, | |
"value": "false" | |
} | |
}, | |
{ | |
"exchange": "C", | |
"price": { | |
"id": 2, | |
"value": "false" | |
} | |
}] | |
def fixup(adict, k, v): | |
if isinstance(adict,list): | |
for item in adict: | |
fixup(item,k,v) | |
elif isinstance(adict,dict): | |
for key in adict.keys(): | |
if k == key and adict["id"] == 2: | |
adict[key] = v | |
if isinstance(adict[key],dict): | |
fixup(adict[key], k, v) | |
fixup(d,"value","true") | |
pprint.pprint(d) | |
# result: | |
#[{'exchange': 'B', 'price': {'id': 1, 'value': 'false'}}, | |
# {'exchange': 'C', 'price': {'id': 2, 'value': 'true'}}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment