GOAL
How to replace a string inside a python dictionary using regex?
SCRIPT
db_result = cursor.fetchall() # databse
lds_data = {} # dictonary
regex = re.compile(r'^([^: \\]+)')
for lds_item in db_result:
lds_data.update({re.findall(regex, lds_item[1]) : {'cust_code' : lds_item[0], 'osplatofrm' : lds_item[2]]}})
ERROR - OUTPUT
TypeError: unhashable type: 'list'
DB_RESULT
CODE_CLIENT HOSTNAME OSPLATFORM
1 SRVDATA:23 WINSERVER
FINAL EXPECTED
{SRVDATA : {'CUST_CODE': 1, 'OSPLATFORM': 'WINSERVER'}
re.findallreturns a list which can not be used as a dict keyre.findall(regex, lds_item[1])returns a list, which you then try to set as a key in your dictionary. this fails as dict keys must be immutable objects (strings, ints, etc.)db_resultand the final expected result. What is the goal to use a sequence as a dict key?