Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

replace underscore by whitespace in python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading