Home > database >  replace underscore by whitespace in python
replace underscore by whitespace in python

Time:02-25

I'm trying to replace every underscore '_' by a whitespace in a list of a list of a dictionary using this code:

d=[[{"30": "PRIORITY"}, {"2022:02:25-12:06:09": "TIMESTAMP"}, {"tester": "HOSTNAME"}, {"named": "APPNAME"}, {"3456": "PID"}, {"resolver_priming_query_complete": "ACTION"}]]
f=''
def Regler(d):
    for i in d:
        for j in i:
            for k in j.keys():
                if('_' in k):
                    k=k.replace('_', ' ')
                    print(d)
Regler(d)

what i want as an output is the same input but just replace the underscore with a white space , i'm not sure where what i missed in my code Thank -you

CodePudding user response:

It's better just to write a new version of dictionary instead of modifying the existing one

d=[[{"30": "PRIORITY"}, {"2022:02:25-12:06:09": "TIMESTAMP"}, {"tester": "HOSTNAME"}, {"named": "APPNAME"}, {"3456": "PID"}, {"resolver_priming_query_complete": "ACTION"}]]
f=''
def Regler(d):
    modified_input = []
    for i in d:
        for j in i:
            temp_dict = {}
            for k, value in j.items():
                if('_' in k):
                    k=k.replace('_', ' ')
                temp_dict[k] = value
            modified_input.append(temp_dict)
        return [modified_input]

print(Regler(d))
  • Related