So I have a huge list of dictionaries and I want to:
- change asterixs into the integer "4"
- change all spaces "" to the integer "0"
- change all other numbers to integers
(this is only a portion of the list)
clean_dict= [{'origin': 'Various/Unknown', 'idps': '', 'year': '1951', 'total': '180000', 'stateless': '', 'ret_refugees': '', 'asylum': '', 'country': 'Australia', 'others': '', 'ret_idps': '', 'refugees': '180000'}, {'origin': 'Various/Unknown', 'idps': '', 'year': '1951', 'total': '282000', 'stateless': '', 'ret_refugees': '', 'asylum': '', 'country': 'Austria', 'others': '', 'ret_idps': '', 'refugees': '282000'}]
I tried this code but nothing happened, any help is very appreciated!
Also pandas library is not allowed for this assignment.
for name, datalist in clean_data.iteritems():
for datadict in datalist:
for key, value in datadict.items():
if value == "*":
datadict[key] = int(4)
elif value == "":
datadict[key]= int(0)
else:
value == int(value)
*to4if the key contains*or if the key is*? Your question would be clearer if you provided sample input which contained all the required characters to be changed (I don't see any asterisks or spaces in your input dict) and the required output! Also space would be' 'not'', which is an empty string.