I have a dictionary where i want to rename some of the dictionary keys which are lower case to camel case, but i want to maintain the sequence. Is there a way to maintain the integrity?
I am writing a cleanser which will aid with the same and it looks like this
def key_cleanser(data):
name_cleanse_dict = {
'id': 'ID',
'emailid': 'emailId',
'firstname': 'firstName',
'middlename': 'middleName',
'lastname': 'lastName',
'addressline1': 'addressLine1',
'addressline2': 'addressLine2',
'addressline3': 'addressLine3',
'postalcode': 'postalCode',
}
if isinstance(data, dict):
dictionary = data.copy()
dict_keys = list(dictionary.keys())
for key in dict_keys:
if key in name_cleanse_dict.keys():
dictionary[name_cleanse_dict[key]] = dictionary.pop(key)
key = name_cleanse_dict[key]
return dictionary
return data
Lets say my dictionary looks like this
person_data = {
'id': 5,
'firstname': 'Bob',
'lastname': 'Woolmer',
'gender': 'Male',
'country': 'USA'
'emailid': 'abs@gmail.com'
}
I am getting the following output:
{
'gender': 'Male',
'country': 'USA',
'ID': 5,
'firstName': 'Bob',
'lastName': 'Woolmer',
'emailId': 'abs@gmail.com'
}
The output I want is:
{
'ID': 5,
'firstName': 'Bob',
'lastName': 'Woolmer',
'gender': 'Male',
'country': 'USA',
'emailId': 'abs@gmail.com'
}
Is there a way to maintain a sequence in case I am replicating this for a dynamic dictionaries.